• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we have array of ArrayLists

 
Deepika Namasani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
can we have array of ArrayList (from java.util; Collection classes)

if so how to declare it

if i want to declare an array of ArrayList of size 3. will this work



Please kindly help me

thanks
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can have. Declare it like this.

ArrayList []ar = new ArrayList[3];

If you declare any array all values in the array
gets initialized automatically, but hence this is
an array of references all the values are initilized to null.
[ June 24, 2005: Message edited by: Srinivasa Raghavan ]
 
Deepika Namasani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Srinivas

can you please let me know y am i getting null pointer exception at this point

" ar[k2-1].add(new Integer(input[i]));"

from the following code.



thanks
Deepika
 
shetal bansal
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven`t intialized the array with ArrayList, so you are getting the NullPointerException.
you have to put,
ar[k2-1]=new ArrayList();
after the line j = newValue;

Hope it helps!!
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i said in my previous post ,

If you declare any array all values in the array
gets initialized automatically, but hence this is
an array of references all the values are initilized to null.

So initilize the arraylist and add datas to it.
 
Deepika Namasani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much that works fine

this is a nice way of learning

thanks to forums
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic