Reputation: 347
I am trying to create different circles with List
and ArrayList
:
List<Circle> views = new ArrayList<Circle>();
for(int i = 1; i < 5; i++) {
views.add(new Circle());
}
But when I use a for loop to get the circles:
Random rand=new Random();
int a,b;
for(int k=1;k<5;k++){
a=rand.nextInt(400)+20;
b=rand.nextInt(400)+20;
views.get(k).setCenterX(a);
views.get(k).setCenterY(b);
views.get(k).setRadius(10);
views.get(k).setFill(Color.DARKRED);
}
It shows me an error.
Upvotes: 4
Views: 1006
Reputation: 159341
After you add circles to your list, you can get them by index. The convention is programming is that indices start at 0, not at 1.
To understand why, see the related question:
Let's say you run your program:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class IndexError extends Application {
@Override
public void start(Stage stage) throws Exception {
List<Circle> views = new ArrayList<Circle>();
for(int i = 1; i < 5; i++) {
System.out.println("Adding circle at index " + (i -1));
views.add(new Circle());
}
Random rand=new Random();
int a,b;
for(int k=1;k<5;k++){
System.out.println("Getting circle at index " + k);
a=rand.nextInt(400)+20;
b=rand.nextInt(400)+20;
views.get(k).setCenterX(a);
views.get(k).setCenterY(b);
views.get(k).setRadius(10);
views.get(k).setFill(Color.DARKRED);
}
stage.setScene(new Scene(new Group(FXCollections.observableArrayList(views))));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Output will be:
Adding circle at index 0
Adding circle at index 1
Adding circle at index 2
Adding circle at index 3
Getting circle at index 1
Getting circle at index 2
Getting circle at index 3
Getting circle at index 4
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at gui.IndexError.start(IndexError.java:33)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
This is because you are trying to get a circle at an index which does not exist. You have only added 4 circles with an index range of 0, 1, 2, 3; so the highest index available is 3).
Instead of looping starting at 1, try looping starting at 0. Likely then the application will start to behave as you expect.
For instance, replace the start method, with the method as below:
public void start(Stage stage) throws Exception {
List<Circle> views = new ArrayList<Circle>();
for(int i = 0; i < 5; i++) {
System.out.println("Adding circle at index " + i);
views.add(new Circle());
}
Random rand=new Random();
int a,b;
for(int k=0;k<5;k++){
System.out.println("Getting circle at index " + k);
a=rand.nextInt(400)+20;
b=rand.nextInt(400)+20;
views.get(k).setCenterX(a);
views.get(k).setCenterY(b);
views.get(k).setRadius(10);
views.get(k).setFill(Color.DARKRED);
}
stage.setScene(new Scene(new Group(FXCollections.observableArrayList(views))));
stage.show();
}
Output is:
Upvotes: 2