I'm trying to save the generated arraylist from my loop into a separate arraylist. It won't allow me to do it; I'm getting an error:
public static void ranCentroid() {
Random randomPoint = new Random();
Cent = new ArrayList<>();
for (int i = 0; i < numCen; i++) {
int randomP = randomPoint.nextInt(Points.size());
System.out.println(Points.get(randomP));
Cent.get(i).add(randomP);
}
System.out.println(Cent);
}
Getting Error
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at phase1.Main.ranCentroid(Main.java:100)
at phase1.Main.main(Main.java:41)
ArrayList
. Which are you working with? It makes rather a large difference to what you need to do.Cent
is still empty, soget
will not work... need to add some (numCen
) new lists to it first - Also would be nice to usearray
for arrays (...[]
) and not for lists, confusing a lot of people