File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why is the ArrayList Object not Created Befor assigning values from ArrayList loc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why is the ArrayList Object not Created Befor assigning values from ArrayList loc" Watch "Why is the ArrayList Object not Created Befor assigning values from ArrayList loc" New topic
Author

Why is the ArrayList Object not Created Befor assigning values from ArrayList loc

Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167



The above Class in the method setLocationCells() assigns the string value loc without creating a ArrayList object or using the add method, In this scenario what happens?Is this like declaring a array variable and assigning values?


Varuna
[ October 02, 2008: Message edited by: Varuna Seneviratna ]

Varuna Seneviratna
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Please use a more informative title for your threads than "what is the difference."

No, it is nothing like creating an array and assigning a member at all. You are not assigning the String value loc because loc isn't a String.

The field loc is an ArrayList<String> so you have to create an ArrayList<String> reference somewhere else and put that reference to an ArrayList<String> into that set method and into the loc reference. The ArrayList<String> will contain as many or as few Strings as were put into it "somewhere else," but you could of course change that after it is passed.
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
Do you mean that a ArrayList<String> object should be created somewhere else, assign values or strings to it, pass it to setLocations method. this will result in locationCells pointing to a ArrayList String type Object?


Varuna
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Yes.
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
Thanks Richie

Varuna
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is the ArrayList Object not Created Befor assigning values from ArrayList loc
 
Similar Threads
Help With With HeadFirst Java Chap.6
While working from Head first java, I came across this problem... Please help
game compile problems
HeadFirst DotComBust... Help?
Head First Java (2nd Ed.) p. 139