java - 列表循环JavaFx

标签 java arraylist javafx-8

我正在尝试使用 ListArrayList 创建不同的圈子:

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 开始。

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

假设您运行程序:

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

尝试从 0 开始循环,而不是从 1 开始循环。然后应用程序可能会开始按您预期的方式运行。

例如替换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();
}

输出是:

image

关于java - 列表循环JavaFx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36322244/

相关文章:

java - 如何使用正则表达式删除电子邮件地址中的点 (.) 字符

java - 使用迭代器 - java.util.ConcurrentModificationException

java - 从 Integer java 列表的列表中查找第一个元素

java - 将 3 个数组列表合并为一个

JavaFX8 - 使用 Guice 的线程任务

java - 获取 javax.crypto.IllegalBlockSizeException : Input length must be multiple of 16 when decrypting with padded cipher?

java - 删除不适用于 HTC 的通话记录

android - 使用android.support.v4.app.ListFragment时列表背景颜色在滚动时变为白色;

java - 如何填充 TableView,其中我可以在 javafx 中使用单选按钮

JavaFX:加载数据任务与进度条结合