• Post Reply Bookmark Topic Watch Topic
  • New Topic

Looping through ArrayList  RSS feed

 
Jayden Forbes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

I have a class that adds objects to an ArrayList and another class that displays whatever is inside the list. Unfortunately, when I call on the display method it always give the result in the end of the List.

The codes below is the class that contains the method that adds and displays the arraylist. Even by using other ways (the commented loops) of getting the elements in the List same results are still being produced.


The code below is my TEST MAIN that will call the class and methods above.




after calling the GroomService.record, am getting:


5
5
5
5
5
5


What would be a better way of doing this?
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Jayden Forbes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response Carey.

Yes you are correct. It only creates 1 instance of the GroomTransDto and updates it as the for loop runs. Within that loop, it updates the value and pass the object that is added list through the line below:



I have tested that section through the code below and it adds the value to the list:





It is looping through the list after the adding them that returns only the value of the last element added:




Do you have any idea why?? Or am I understanding the codes differently? LoL
 
Jayden Forbes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch


Thank you
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want "unique" values you must add "unique instances". That is, for every call to add(), create a new object.
Your approach doesn't work because when you call setUser_id() you aren't creating a new object with a new id, what you are doing is assigning (overwriting) a new id to an old object.

As a way to keep yourself from making this mistake you could make your class "immutable". You would do this by eliminating any "set" methods in the class (e.g. setUser_id()) and pass in all necessary parameters (e.g. ID) in the constructor.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:

Note that when you "add" your object, you are adding a reference to the object you've already created. You are not adding a copy of your object. Hence, your print loop will print out the same object N times and the id will whatever id was set last.
 
Jayden Forbes
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thank you for clarifying. I just needed to create a new object whenever I add it to the list.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!