aspose file tools*
The moose likes Beginning Java and the fly likes Create Object in DoWhile Send to Arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Create Object in DoWhile Send to Arraylist" Watch "Create Object in DoWhile Send to Arraylist" New topic
Author

Create Object in DoWhile Send to Arraylist

Brian Lemieux
Greenhorn

Joined: Dec 02, 2004
Posts: 6
Running a command line program. I am including just the things in the pgm that I think is needed to answer this question.



/*In the while loop an object is created using the input.next with answers from the user and sending the info to the constructor of a class which creates an object. In the loop then I want to add the numb to the arraylist x each time. Would this be the code next in line to get the object into the arraylist or can I do that?

and would the object numb be added each position in the arraylist be ok or would the object need to be named something else since it is a reference and all the numbs in the arraylist would point to the same object? If this can't be done then how to I name the next object in line something else the way it is?
*/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

"brian39", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36499
    
  16
"brian39" please read the important administrative private message I just sent you.

CR
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36499
    
  16
Originally posted by Bear Bibeault:
"brian39", please check your private messages for an important administrative matter.


Snap!
Brian Lemieux
Greenhorn

Joined: Dec 02, 2004
Posts: 6
To all:

My publicly displayed name was not correct. I have since corrected it. They were right on it. Shame on me.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

No prob. Thanks for the quick action.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Originally posted by brian39:
and would the object numb be added each position in the arraylist be ok or would the object need to be named something else since it is a reference and all the numbs in the arraylist would point to the same object?*/


Your code is fine.

numb is a reference to an object. When you callyou add a copy of that reference to the ArrayList, so you now have two references to the same object (numb and the first element in the ArrayList). The next time round the loop, whenis executed, numb will now point to the new object and the first element of the ArrayList will still point to the first object you created. You then call again. You now have two references to the second object you created (numb and the second element of the ArrayList) and one reference to the first object you created (the first element of the ArrayList) and so on and so on.


Joanne
Brian Lemieux
Greenhorn

Joined: Dec 02, 2004
Posts: 6
I did not think there could be a reference variable with the same name. To me it means it would be pointing to the same object. In this case it appears from what I am understanding that the references even though having the same reference name will be pointing to a different object. Ok!

Thanks!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Originally posted by Brian Lemieux:
I did not think there could be a reference variable with the same name. To me it means it would be pointing to the same object. In this case it appears from what I am understanding that the references even though having the same reference name will be pointing to a different object. Ok!

Thanks!


Okay, I see what you are saying - because you have the declaration of numb inside the loop a new local variable does get created each time, but the old ones are always discarded. You only ever have one numb variable. There isn't actually any need to declare numb each time - you could just declare it once before the loop
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Create Object in DoWhile Send to Arraylist
 
Similar Threads
need help using StringTokenizer
switch statement
Head First Java Five Minute Mystery p. 268
Hash Map to do Inverted Index?
Html tag content wrapping