I need to create multiple objects in a loop. I read elsewhere that adding them into a list would accomplish this task, but the below code gives me a set of copies of the same object, i.e. all with the same values. Any idea how I can create multiple objects, rather than just copies of the same one? Thank you.
(The code below is a simplified version of what I'm working on)
System.out.println("Creating swarm of size "+swarmSize);
List<Dog> myDogs = new ArrayList<Dog>();
for(int i = 0; i < dogAmount; i++) {
System.out.println("New Dog # "+i);
myDogs.add(new Dog(i));
}
Dog first = myDogs.get(0);
Dog other = myDogs.get(3);
System.out.println(first.getID()+" "+other.getID());
//prints out the number of dogs I should have created -1 both times
My Dog class
import java.util.*;
public class Dog{
public static int dogID;
public Dog(int ID) {
dogID = ID;
}
public int getID() {
return dogID;
}
public void setID(int id) {
dogID = id;
}
}