This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can we have array of  ArrayLists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can we have array of  ArrayLists" Watch "can we have array of  ArrayLists" New topic
Author

can we have array of ArrayLists

Deepika Namasani
Greenhorn

Joined: May 19, 2005
Posts: 25
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


Deepika N
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
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 ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Deepika Namasani
Greenhorn

Joined: May 19, 2005
Posts: 25
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

Joined: May 09, 2005
Posts: 63
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

Joined: Sep 28, 2004
Posts: 1228
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

Joined: May 19, 2005
Posts: 25
Thank you very much that works fine

this is a nice way of learning

thanks to forums
 
GeeCON Prague 2014
 
subject: can we have array of ArrayLists