• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Create Object in DoWhile Send to Arraylist

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
*/
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"brian39", please check your private messages for an important administrative matter.
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"brian39" please read the important administrative private message I just sent you.

CR
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
"brian39", please check your private messages for an important administrative matter.



Snap!
 
Brian Lemieux
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To all:

My publicly displayed name was not correct. I have since corrected it. They were right on it. Shame on me.
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No prob. Thanks for the quick action.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Brian Lemieux
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic