Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java Swing中创建一行,该行的长度等于数组中的int_Java_Arrays_Swing_For Loop - Fatal编程技术网

在Java Swing中创建一行,该行的长度等于数组中的int

在Java Swing中创建一行,该行的长度等于数组中的int,java,arrays,swing,for-loop,Java,Arrays,Swing,For Loop,我试着画一个以10为间隔的直线的forloop,我希望这些直线是数组中int数的长度。当我尝试将它们的大小设置为数字[I]时,它会给我一个数组索引越界错误。数组的大小为50,并且填充了20-100之间的随机数 private void drawPass(Graphics g) { int space = 10; for(int i = 0; i <= numbers.length; i++) { g.drawLine(space, 1000, numbers

我试着画一个以10为间隔的直线的forloop,我希望这些直线是数组中int数的长度。当我尝试将它们的大小设置为数字[I]时,它会给我一个数组索引越界错误。数组的大小为50,并且填充了20-100之间的随机数

private void drawPass(Graphics g) {
    int space = 10;
    for(int i = 0; i <= numbers.length; i++) {
        g.drawLine(space, 1000, numbers[i], numbers[i]);
        space += 10;
    }
}
private void drawPass(图g){
int空间=10;

对于(int i=0;i如果数组的长度为50,那么您可以访问的最后一个索引是49,因为数组的索引从0开始。在您当前的情况下,您尝试访问不存在的索引50(即
numbers.length

改变

i <= numbers.length;

你的问题解决了吗?如果没有,你能提供一些反馈吗?
i < numbers.length;
[5, 8, 3, 4, 9]   // Random array of ints   (  length   = 5)
 0  1  2  3  4    // Index of each position (last index = 4)