Nachdem Sie Kreise zu Ihrer Liste hinzugefügt haben, können Sie sie nach Index abrufen. Die Konvention ist die Programmierung ist, dass Indizes bei 0, nicht bei 1 beginnen
Um zu verstehen, warum, sehen die damit verbundene Frage:
Angenommen, Sie haben Ihr Programm ausführen:
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);
}
}
Ausgang wird sein:
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)
Das ist, weil Sie versuchen, einen Kreis bei einem Index zu erhalten, der nicht vorhanden ist. Sie haben nur 4 Kreise mit einem Indexbereich von 0, 1, 2, 3 hinzugefügt. Daher ist der höchste verfügbare Index 3).
Anstatt Schleife von 1 beginnend, versuchen Sie, Schleife bei 0 beginnen. Wahrscheinlich wird dann die Anwendung wie erwartet funktionieren.
Zum Beispiel, ersetzen Sie das Startverfahren, mit dem Verfahren, wie unten:
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();
}
Ausgang ist:
Was ist der Fehler? –
Nur darauf hinweisen, falls Sie es nicht bemerkt haben: 'int i = 1; i <5; i ++ 'schleift nur 4 mal, nicht 5, da es nur' 1, 2, 3 und 4' ist. Vielleicht ist das was du willst, vielleicht nicht. #justsaying zeigen Bitte geben Sie die Fehlermeldung Sie haben, :) – Tacocat
es eine Ausnahme – Ilyes