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

New Topic/Question
Reply




MultiQuote




|