This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 38087
    
  22
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: 38087
    
  22
Yes.
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
Thanks Richie

Varuna
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
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
game compile problems
While working from Head first java, I came across this problem... Please help
Help With With HeadFirst Java Chap.6
HeadFirst DotComBust... Help?
Head First Java (2nd Ed.) p. 139