Sparkles

that were shone when I got tempered!

Why it cannot be to create an Array of generic ArrayLists (eg : ArrayList[])?

leave a comment »

Why first try says “Cannot create a generic array of ArrayList<String>”.

ArrayList<String>[] arryListArry = new ArrayList<String>[4];

Answer :

If the first one is allowed to do, followig will be happend.

ArrayList<String>[] alist = new ArrayList<String>[10];
Object[] olist = alist; // This is okay because ArrayList is an Object
olist[0] = new ArrayList<Dog>();
olist[0].add(new Dog());
String s = alist[0].get(0); //Uh oh, Dog is not string

Ok versions :

ArrayList[] arryListArry = new ArrayList[4];
ArrayList<String>[] arryListArry = new ArrayLis[4];
ArrayList<ArrayList<String>> arryListArry= new ArrayList<ArrayList<String>>(4);
Advertisements

Written by Namal Fernando

October 22, 2013 at 7:35 am

Posted in Java, Q&A

Tagged with , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Ruth's Reflections

Contemplations from quakey quirky Christchurch

TED Blog

The TED Blog shares interesting news about TED, TED Talks video, the TED Prize and more.

Ziplok

Learn and discover simple things

Meihta Dwiguna Saputra's Knowledge Base

~In learning you will teach and in teaching you will (re)learn~

The Java Blog

Thoughts, tips and tricks about the Java programming language

Sparkles

that were shone when I got tempered!

%d bloggers like this: