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.
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.
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.
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) .
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)?
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>.
I merged your stuff with the following thread. I hope that is okay by you.
posted 1 year ago
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 is 1.Then the Vnf which the value is 2,30 is added to List of "requiredVNFTypeChain"
The result now is
chain 0=[2, 30, 1, 20]
chain 1=[2, 30, 1, 20]
But the expected result is
chain 0=[1, 20]
chain 1=[2, 30]