This code will not compile for me , can anyone fix it or see a problem with it?
I keep getting the error arraylistv2.java uses unchecked or unsafe operators
,
when i added -Xlint it pointed to
private T[] seq = (T[])(new Object[1024]); // 1024 arbitrary
as the problem , any ideas?
class ArrayList<T>
{
private T[] seq = (T[])(new Object[1024]); // 1024 arbitrary
private int numItems = 0; // seq[0..numItems-1] significant
public int size() { return(numItems); }
public T get(int i)
{
if(i < 0 || i >= numItems)
throw new IndexOutOfBoundsException();
else
return seq[i];
}
public T set(int i, T t)
{
if(i < 0 || i >= numItems)
throw new IndexOutOfBoundsException();
else
{
T temp = seq[i];
seq[i] = t;
return temp;
}
}
public boolean add(T t)
{
add(numItems,t);
return true; // for compatibility reasons only
}
public void add(int i, T t)
{
if(i < 0 || i > numItems)
throw new IndexOutOfBoundsException();
if(numItems == seq.length)
resize(); // extend seq
for(int k = numItems; k > i; k--) // shift seq[i..] to right
seq[k] = seq[k-1];
seq[i] = t;
numItems++;
}
private void resize()
{ // seq is full -- double its size
T[] temp = (T[])(new Object[seq.length * 2]); // bigger array
for (int i = 0; i < seq.length; i++) // copy over items
temp[i] = seq[i];
seq = temp;
}
}
public class arraylistv2{
public static void main(String [] args){
ArrayList<String> arraylist1 = new ArrayList<String>();
arraylist1.add(1,"orange");
arraylist1.add(2,"apple");
int i = arraylist1.size();
System.out.println(arraylist1);
}
}