Array of ArraList?

Type erasure makes java generics really sucky. The following is legal, but issues a warning if not suppressed.

@SuppressWarnings(“unchecked”)
ArrayList[] ai = new ArrayList[somenumber];

What’s happening is that you have a setting that’s treating a warning as an error.

No, that’s different.

That, my friend, is technically a form of type casting (To a point). You’re not making an ArrayList[], you’re making an ArrayList[], and then making the program treat it as if it’s an ArrayList.

Of course, for all intents and purposes that’s alright. But it’s technically not safe. The classic example is something like…


        ArrayList[] basicList = new ArrayList[10];
        ArrayList<Number>[] numberList = basicList;
        ArrayList<String>[] stringList = basicList;
        numberList[0] = new ArrayList<Number>();
        numberList[0].add(10);
        stringList[1] = new ArrayList<String>();
        stringList[1].add("Hello!");
        try {
            System.out.println(stringList[0].get(0).length());
        } catch (ClassCastException cce) {
            cce.printStackTrace();
        }
        try {
            System.out.println(numberList[1].get(0).intValue());
        } catch (ClassCastException cce) {
            cce.printStackTrace();
        }

Which results in:


java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
        at sorting.Node.main(Node.java:40)
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
        at sorting.Node.main(Node.java:45)

Look at the source code of ArrayList.

Yep.

I know. It only works because the ObjectType[] is never made available. If you ensure that you can’t do what I put just below, then your fine.

It works, yes. But it’s not guaranteed to be safe.

Problem cases are identical.

This way works. It’s a simpler form from above.

      ArrayList<String>[] str = new ArrayList[9];
      for (int i = 0; i < str.length; i++) {
         ArrayList<String> arrayList = new ArrayList<String>();

      }

This. Either all generics or all arrays.


ArrayList<ArrayList<Object>> bigList = new ArrayList<ArrayList<Object>>();
for (ArrayList<Object> currentInnerList : bigList)
{
	currentInnerList = new ArrayList<Object>();
}

I just use arrays though.

Hey! Awesome input, many thanks to all! :slight_smile:

I would like to put a hold on this thread, unless you’d like to keep discussing best programming techniques.

As for me, I’m redesigning the whole project.

Thank you all once again, I’ve bookmarked this already for future reference! :smiley:

PS: You guys rock! :-*

That doesn’t work, you are only assigning the local variable “currentInnerList” the ArrayList object :wink:

@UprightPath
As I told you on IRC too, I just realized that the code I posted works in C#, which is where I used it and thought it was in Java. My bad on that part :slight_smile:

ah yes. I don’t use for each that often - well just use a normal for loop then

edit: actually I am thinking too much of arrays which I use normally. Of course you dont even have to iterate through here at initialization, because its still empty; being an empty list and an array which has a bunch of null pointers, is of course not the same…