简体   繁体   中英

Where is the error? Java, error looping over 2D array of booleans

I am working on a Java personal programming project and am new to Java. However not new to for loops with which I have a problem in this particular class. I'm trying to loop over a 2D array of booleans. The array is a square matrix of length 5. so I'm trying to start at a value of i = 0 to i = 4. Java however is giving me the following error:

run:
true
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
false
false
false
false
    at tetris2.pkg0.Piece.main(Piece.java:42)
/home/chihabgoku/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

The code of my class is as follows:

package tetris2.pkg0;

public class Piece {

    private int x;
    private int y;
    private boolean[][] sg;

    public Piece(int xx, int yy, boolean[][] sg){
        this.x = xx;
        this.y = yy;
        this.sg = sg;
    }

    @Override
    public String toString(){
        String res = "(";
        for(int i = 0; i < this.sg.length; i++){
            res += '(';
            for(int j = 0; j < this.sg[0].length; j++){
                if(j < this.sg.length - 1){
                    res += this.sg[i][j] + ", ";
                }else{
                    res += this.sg[i][j];
                }
            }
            res += "), ";
        }
        return res += this.x + ", " + this.y + ")";
    }

    public static void main(String[] args){
        int sgSize = 5;
        boolean[][] sg = new boolean[][]{{true, false, false, false, false}, 
                                         {true, false, false, false, false}, 
                                         {true, false, false, false, false}, 
                                         {true, false, false, false, false}, 
                                         {true, true, true, false, false}
                                        };
        for(int i = 0; i < sgSize - 1; i++){
            for(int j = 0; i < sgSize - 1; j++){
                System.out.println(sg[i][j]);
            }
        }

        Piece p = new Piece(0, 0, sg);
        System.out.println(p);

    }
}

When I get rid of the following part, everything works fine.

        for(int i = 0; i < sgSize - 1; i++){
            for(int j = 0; i < sgSize - 1; j++){
                System.out.println(sg[i][j]);
            }
        }

Does anyone see the error?

Thank you!

It's there:

for(int i = 0; i < sgSize - 1; i++){
    for(int j = 0; i < sgSize - 1; j++){
        System.out.println(sg[i][j]);
    }
}

The inner for tests 'i' instead of 'j'. Try with:

for(int i = 0; i < sgSize - 1; i++){
    for(int j = 0; j < sgSize - 1; j++){
        System.out.println(sg[i][j]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM