Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

copy objects by value java

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have Class IoT with two variables xLocation and yLocation. In this Class I have constructor and setter and getter for the class.



I have another class named as main with the method main.In this class, I set a random value to xlocation and ylocation for object of IoT class.And then add these value to the list which is named as IoTList and list is a kind of IoT .When I print the value of list by getxLocation.All the value of the list are the same.How can I solve this problem?I want to have different value in the list.



 
Bartender
Posts: 7196
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because your new IoT() is outside the loop your IoTdevice refers to the same object over and over. You are thereby overwriting your IoTdevice locations with your setters and ALL the IoTdevice's added to your list refer to the same object. So, last setter calls win.
 
Negar Afrasiabi
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for quick response.In this example I can write new IoT() inside the loop. what about this example.
In this example I have a class Nodes with a variable Id and two ArrayList.I have setter,getter and constructor inside the class.



I have another class which is Main with the main method.In this class I have 2 ArrayList with size 10 and I add random number inside the list. I create an array of object "nodes[]"for class Nodes which initialize the value for them.Again in this example when I want to print all the xlocation for object nodes.All the value of the object for the  list are the same.How can I solve this problem?I want to have different value for the different object.

 
Carey Brown
Bartender
Posts: 7196
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you call setxLocation for nodes that you've already passed in the xLocation when you called the constructor?
 
Carey Brown
Bartender
Posts: 7196
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Negar Afrasiabi
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct I can remove it but still I have that problem. And this I can not define "new Nodes" inside the loop as I can have other variable inside the Nodes class which I want to initialize the value manually not likes random(randomly generate) or id(increase each time by for loop) .

 
Carey Brown
Bartender
Posts: 7196
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Bartender
Posts: 7196
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are pre-allocating space for 10 entries in your ArrayList, yet your loops add 11 entries. It would be better not to pre-allocate at all. And, do you really want your loop to loop 11 times (0-10 inclusive)?
 
Negar Afrasiabi
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I print the value of object of class Nodes.


The result is likes below. The xlocation is random value. I except the xlocation for each ID is different.

ID=1 xLocation[23, 25, 8, 2, 33, 31, 32, 36, 30, 43, 40] yLocation[18, 19, 26, 15, 21, 10, 39, 34, 20, 37, 16]
ID=2 xLocation[23, 25, 8, 2, 33, 31, 32, 36, 30, 43, 40] yLocation[18, 19, 26, 15, 21, 10, 39, 34, 20, 37, 16]


 
Carey Brown
Bartender
Posts: 7196
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are reusing the Lists, of course they'll be the same.

This is where a helper method would be useful:

Please note that my example is repeating one of your bad habits in that the destination type should be "List" and not "ArrayList". Example
You should be programming to a higher level of abstraction to ease program changes in the future. An ArrayList is a concrete implementation of an abstract List type. By using List<Integer> you can then change from ArrayList to LinkedList without affecting your code because a LinkedList is also a List.
So, getRandomListOfIntegers() should really return List<Integer>.
 
Marshal
Posts: 69790
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Negar Afrasiabi
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have Class VnfInstances with two variables trafficAmount and vnfType. In this class I have constructor and setter & getter for the variables.




I have another class which is Chain.In this class have a variable Id and a List named as requiredVNFType which is a kind of "VnfInstances" Class.




The third class is the Main class. In this class the array of object for two classes "VnfInstances" and "Chain" is made and the value are initialize for them.

To clarify the if clause meaning I provide the example.for example if the id of chain[1] is 1.Then the Vnf[1] which the value is 2,30 is added to List of "requiredVNFTypeChain"
The result now is
VNF
1, 20
2, 30
3, 10
chain 0=[2, 30, 1, 20]
chain 1=[2, 30, 1, 20]
But the expected result is
1, 20
2, 30
3, 10
chain 0=[1, 20]
chain 1=[2, 30]





 
Rancher
Posts: 4614
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As with your other thread, you are using the same List in both instances of Chain, so when you add something to that List it will be present in the output for each Chain:

That above is the same as simply going:
 
Campbell Ritchie
Marshal
Posts: 69790
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:As with your other thread . . .

I think I should merge the two threads into one.
 
    Bookmark Topic Watch Topic
  • New Topic