Generics and ArrayList warnings

Just need some help with these two warnings driving me insaine

Page 1 of 1

1 Replies - 1613 Views - Last Post: 07 April 2007 - 07:18 PM Rate Topic: -----

#1 Suspect1  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 19-March 07

Generics and ArrayList warnings

Posted 07 April 2007 - 05:47 PM

ok here are the two warnings i'm getting


C:\**********>javac -Xlint:unchecked Lab12.java
Lab12.java:34: warning: [unchecked] unchecked call to add(E) as a member of the
raw type java.util.ArrayList
Box.add(obj);
^
Lab12.java:72: warning: [unchecked] unchecked cast
found : java.lang.Object
required: T
T obj = (T)Box.get(i);
^
2 warnings


How am i able to stop these annoying warnings, any help would be great thanks!


 /**
 * Lab12.java
 *
 * This program simulates drawing an item at random out of a box.
 * It uses a generic class with a parameter of type T so that
 * a RandomDrawing may be conducted with any object type.  An
 * ArrayList is used to store the objects in the box.
 */

import java.util.ArrayList;
import java.util.Random;


class RandomDrawing<T>
{
	/**
	* In the constructor we create a new, empty box, via the ArrayList.
	*/
	public RandomDrawing()
	{

	Box = new ArrayList();

	}

	/**
	* @param obj - Item to add to the box
	*/
	public void add(T obj)
	{	

	Box.add(obj);

	//.....
	}

	/**
	* @return boolean True if the box is empty (had no items in it).
	*/
	public boolean isEmpty()
	{

	return Box.size() == 0;
		//....
	}

	/**
	* clear the entire ArrayList
	*/
	public void clear()
	{

	Box.clear();
		//....					 // clear() from ArrayList
	}

	/**
	* @return T Item that is randomly selected (and removed) from the box,
	* or null if the box is empty.
	*/
	public T drawItem()
	{
	 if(isEmpty())
		{
			return null;
		}
	else
	{
		int i = (int)(randIndex.nextDouble() * (double)Box.size());
		T obj = (T)Box.get(i);
		Box.remove(obj);
		return obj;
	}
	
	}
	
	   //...			 // holds objects in the box
	   //...			 // set the seed at 10
	private ArrayList Box;
	private static Random randIndex = new Random(10L);
	private T Object;
	private T Boxy;
	private int i;

}


public class Lab12
{
	/**
	* main adds various strings to the box and output each one until
	* the box is empty.
	* The order we draw the items out of the boxes should be same upon
	* successive runs of the program because we initialize the private
	* static pseudo random number with the method Math.seed( 10 ).
	*/

	public static void main(String[] args)
	{
		RandomDrawing<Integer> intBox	= new RandomDrawing<Integer>();

		// add some sample values to the integer box
		intBox.add(3);
		intBox.add(10);
		intBox.add(75);
		intBox.add(45);

		// draw from integer box until empty

		System.out.println("drawing from the box of integers:");

		while ( !intBox.isEmpty() )
		{
			System.out.println(intBox.drawItem());
		}

		RandomDrawing<String>  stringBox = new RandomDrawing<String>();

		// add some sample values to the string box
		stringBox.add("Carol");
		stringBox.add("Bob");
		stringBox.add("Ted");
		stringBox.add("Alice");

		System.out.println("\ndrawing from the box of strings:");
		while (!stringBox.isEmpty())
		{
			System.out.println(stringBox.drawItem());
		}

// from here to the end of main(): optional

		intBox.clear();					 // clear ArrayList intBox

		for (int i = 1; i < 54; ++i)
		{
			intBox.add(i);
		}

		System.out.println("\ndrawing quick picks from the box of integers:");
		Integer qpick[] = new Integer[6];
		for (int i = 0; i < 6; ++i)
		{
			int n = intBox.drawItem();
			System.out.printf("%5d", n);
			qpick[i] = n;
		}
		System.out.printf("\n\n");

		GS.sort(qpick, qpick.length);

		for (int i = 0; i < 6; ++i)
		{
			System.out.printf("%5d", qpick[i]);
		}
		System.out.printf("\n");
	}
}



This post has been edited by Suspect1: 07 April 2007 - 05:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Generics and ArrayList warnings

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Generics and ArrayList warnings

Posted 07 April 2007 - 07:18 PM

The collections classes in Java 5.0 and later are parameterized types. Read more about Java Generics: http://java.sun.com/...rics/index.html. If you don't want to deal with this, change your Java version to 1.4.2. I recommend learning about generics, however, as they are not going to go away in future versions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1