0

consider the following class:

package org.cloudbus.cloudsim.ACO;

/**
*
* @author Dinesh Gupta
*/
public class VMLOADFITNESSTABLE {
public int vmid;
public double memoryused;
public double cpuused;
public double fitnessvalue;
public int loadstatus;

};

i have created a list of objenter code hereects for this class using the following state:

List<VMLOADFITNESSTABLE> vlft = new ArrayList<VMLOADFITNESSTABLE>();

then i have added multiple elements to the same list. when i try to display all elements in the list, i observe that the last element is displayed multiple times. it feels like that the elements have been overwritten by the last element.

please suggest some answer,

  • 3
    Welcome, please post a [mcve]. How could we reproduce this behavior ? My lists works fine, so it must be your code – AxelH Sep 29 '17 at 11:10
  • Are you by any chance adding the _same instance_ to the list multiple times? Without a [mcve] we can't tell ... – Thomas Sep 29 '17 at 11:12
  • Java passes it's objects around by reference. Most likely you keep one `Object`, alter it and insert the altered version into the list multiple times, which shows the behavior you described. –  Sep 29 '17 at 11:12
  • This is because you keep adding the same element to list. You have to call `new VMLOADFITNESSTABLE()` for each element that you add to the list. – Sergey Kalinichenko Sep 29 '17 at 11:12

0 Answers0