简体   繁体   English

线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:3

[英]Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3

I'm trying to implement an A* pathfinding algorithm in java. 我正在尝试在Java中实现A *寻路算法 So I create a grid and the user is requested to give its dimensions. 因此,我创建了一个网格,并要求用户提供其尺寸。 The problem is that when width!=height , the program throws that exception. 问题在于,当width!= height时 ,程序将抛出该异常。 For example, a grid 5x5 is created without problems, whereas a grid 5x7 is not. 例如,创建网格5x5不会出现问题,而不会创建网格5x7。 I'm not sure how I can fix it. 我不确定该如何解决。 Here is the code: 这是代码:

JFrame frame = new JFrame();
String rows =  JOptionPane.showInputDialog(frame,
                "Συμπληρώστε τον αριθμό γραμμών του πλέγματος: \n",
                "Δημιουργία πλέγματος",
                JOptionPane.PLAIN_MESSAGE);
String cols =  JOptionPane.showInputDialog(frame,
                "Συμπληρώστε τον αριθμό στηλών του πλέγματος: \n",
                "Δημιουργία πλέγματος",
                JOptionPane.PLAIN_MESSAGE);
int rowsnum = Integer.parseInt(rows); 
int colsnum = Integer.parseInt(cols);
transient Image buffer; 

GridCell gridCell[][] = new GridCell[rowsnum][colsnum];
public Map()
{
    super();
    //{{INIT_CONTROLS
    setLayout(new GridLayout(rowsnum,colsnum));
    //}}
    for(int i=0;i<rowsnum;i++){
        for(int j=0;j<colsnum;j++){
            System.out.println ("i=" + i + " j="+ j);
           gridCell[j][i] = new GridCell();
           gridCell[j][i].setPosition(new Point(j,i));
           add(gridCell[j][i]);
        }
    }
}

So as you see, I'm printing i and j in each loop to see where the problem is. 如您所见,我在每个循环中打印i和j以查看问题出在哪里。 And the result is (when I try to create a grid 3x5): 结果是(当我尝试创建3x5网格时):

run:

i=0 j=0
i=0 j=1
i=0 j=2
i=0 j=3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at Map.<init>(Map.java:32)

Where line 32 is: 第32行是:

gridCell[j][i] = new GridCell();

If anyone could help me I'd be grateful! 如果有人可以帮助我,我将不胜感激!

PS Ignore the greek! 附注:忽略希腊! :-D :-D

On this line you create a 2D array that is rowsnum by colsnum 在此行上,创建一个2D数组,该数组为rowsnumcolsnum

GridCell gridCell[][] = new GridCell[rowsnum][colsnum];

However here you use it as if it is colsnum by rowsnum . 但是在这里,您使用它的方式就好像它是colsnum by rowsnum

for(int i=0;i<rowsnum;i++){
    for(int j=0;j<colsnum;j++){
        System.out.println ("i=" + i + " j="+ j);
       gridCell[j][i] = new GridCell();

i goes between 0 and rowsnum (exclusive) but goes in the dimention that is between 0 and colsnum (exclusive) i在0和rowsnum之间(不包括),但在0到colsnum之间(不包括)

j goes between 0 and colsnum (exclusive) but goes in the dimention that is between 0 and rowsnum (exclusive). j介于0和colsnum之间(不包括colsnum ),但位于0到rowsnum之间(不包括rowsnum )。

Solution

Either i and j are the wrong way round in gridCell[j][i] (and elsewhere) or rowsnum and colsnum are the wrong way round in new GridCell[rowsnum][colsnum] . ijgridCell[j][i] (以及其他地方)中错误的处理方式,或者rowsnumcolsnumnew GridCell[rowsnum][colsnum]方式错误。 Which it is depends on what you're trying to achieve, although conventionally its often array(rows,columns); 它是哪一个取决于您要实现的目标,尽管通常它通常是数组(行,列); implying i and j are the wrong way round 暗示我和j是错误的方法

The issue is with the way row index (i) and col injex (j) are used in your code. 问题在于代码中使用行索引(i)和col injex(j)的方式。 Please interchange i and j in the below code 请在以下代码中交换i和j

       gridCell[j][i] = new GridCell();
       gridCell[j][i].setPosition(new Point(j,i));
       add(gridCell[j][i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 7 - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:6 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 6 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException:2 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:5 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException: - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:-1 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException 4 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 4 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException 线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:8 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 8 线程“主”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0 - Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM