列表循环JavaFx

我试图用List和ArrayList创建不同的圆:

List<Circle> views = new ArrayList<Circle>();
for(int i = 1; i < 5; i++) {
    views.add(new Circle());
}

但是当我使用for循环获取圆圈时:

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);

}

它显示了一个错误.

最佳答案 将圆圈添加到列表后,您可以通过索引获取它们.编程的惯例是索引从0开始,而不是从1开始.

要了解原因,请参阅相关问题:

> Why are zero-based arrays the norm?

假设你运行你的程序:

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);
    }
}

输出将是:

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)

这是因为您试图在不存在的索引处获得一个圆圈.您只添加了4个圈子,索引范围为0,1,2,3;所以可用的最高指数是3).

而不是从1开始循环,尝试从0开始循环.可能然后应用程序将开始按预期运行.

例如,使用以下方法替换start方法:

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();
}

输出是:

《列表循环JavaFx》

点赞