0

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);

    }


}
wolfcastle
  • 5,510
  • 3
  • 32
  • 45
owen95
  • 13
  • 3

1 Answers1

0

The message"arraylistv2.java uses unchecked or unsafe operators" is a warning not an error. In other words, there is no compile time error in your code. So, it should compile just fine (albeit with a couple of warning messages).

There is actually a runtime error in your code:

Exception in thread "main" java.lang.IndexOutOfBoundsException
  at arraylistv2$ArrayList.add(arraylistv2.java:45)
  at arraylistv2.main(arraylistv2.java:5)

Lastly, it seems that the type cast from object[] to T[] is not necessary in your code here:

private T[] seq = (T[])(new Object[1024]);

I'd recommend leaving it as a Object[]. All methods of your custom ArrayList class work with the generic type T so there is no way to put any other type of objects in seq anyways. Here is an example of custom ArrayList class that I wrote some time ago: https://github.com/anshulverma/nuaavee-collections/blob/master/src/main/java/com/nuaavee/collections/list/ArrayList.java

Hope that helps.

P.S. I should also point out that it is usually better to ask specific questions rather than pasting complete code listing. You will have more people responding to your questions that way. Not everyone has a lot of free time like me ;)

nuaavee
  • 1,258
  • 2
  • 15
  • 30
  • Sorry im new , and im still getting warnings when i change that , what exactly should i change the line of code to? I want to make sure i fully understand what you are suggesting that i change , thanks – owen95 Dec 02 '15 at 11:47
  • I meant that you can change `private T[] seq = (T[])(new Object[1024]);` to `private Object[] seq = new Object[1024];` to avoid the warning and only typecast from `Object` to `T` in the `get` method. Regarding warning messages that you are getting, it is ok to leave them be or suppress them using `@SuppressWarnings("unchecked")`. – nuaavee Dec 02 '15 at 14:55