Generic arrays of parametrized ArrayLists in java?

Read chapter 7.3 (page 15) in the official tutorial.

Thanks Roman. The tutorial is tough to follow at first, but the aspect is explained in detail. – athena Mar 28 '10 at 15:54.

Actually the implementation of ArrayList is very efficient for read and write actions that don't change the size of the list. In many cases (at least since java 1.6) the compiler will totally remove the method call overhead (for instance for get()). Also not many programs require the performance that an array offers, so don't worry about using arrays until your code is too slow (and even then you probably don't need the arrays).

If you could do that, this will happen : ArrayList alist = new ArrayList10; Object olist = alist; // This is okay because ArrayList is an Object olist0 = new ArrayList(); olist0. Add(new Dog()); String s = alist0. Get(0); //Uh oh, Dog is not string.

1 This a a fundamental flaw in array covariance and is not the reason for the error. – SLaks? Mar 21 '10 at 22:46.

The reason you have to do that is because generics don't really exist in Java. It's actually a compiler hack. As for the two lines you posted, there shouldn't be any difference in terms of the bytecode produced.

However, in the first case you'll probably get a warning because your compiler thinks you forgot java.util. ArrayList wants a type parameter, whereas the latter won't compile because you're trying to instantiate a generic array. Lose-lose situation :(.

I would also recommend creating an arraylist of arraylists. ArrayList> alist = new ArrayList>(); , where Type is whatever type you wanted the list to be. You now have an arraylist that holds array lists.

If you want to add an arraylist, you can do: alist. Add(new ArrayList()).

1 Even better, List>, an example of coding to the interface. Stackoverflow. Com/questions/2039549/… – trashgod Mar 22 '10 at 4:13 I am keen to know, why is creating Lists of Lists is better, rather than just array of lists.

– athena Mar 22 '10 at 17:56.

I am new to Java, so I am not aware of the nitty gritties. Why can't I create generic array of parametrized ArrayList? I've read similar questions here but I'm still a little confused.

I'm having a problem that I have encountered before, but I still don't know why it happens.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions