arrays - Java ArrayIndexOutOfBoundsException: 14 when using getMatrix() -
this question has answer here:
i have constructor map() attributes sector , matrix creates , array of 23x14 , numerical matrix of 23x14, have constructor mapfermi() sets specific numerical matrix , names each sector according numerical value of each matrix cell, when i've run test of notnull error arrayindexoutofboundsexception on first line of getmatrix method in mapfermi constructor, isn't getmatrix()[i][j] supposed return numeric value of cell[i][j] of numeric matrix set previuosly?
public class map { protected sector [][] sector; private int matrix [][]; private static final int x=23; private static final int y=14; public map (){ sector = new sector[x][y]; (int i=0; < x; i++){ (int j=0; j<y; j++) { sector[i][j] = new sector (i,j); } } matrix = new int[x][y]; } public int[][] getmatrix() { return matrix; } public void setmatrix(int matrix[][]) { matrix = matrix; } }
.
public class mapfermi extends map { public mapfermi() { setmatrix(new int[][]{ {0,0,0,0,0,0,0,0,0,3,0,0,0,3,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,1,0,2,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,2,1,2,1,2,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,1,2,2,2,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,3,0,1,0,3,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,2,1,0,1,0,2,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,2,0,0,0,0,0,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,1,1,1,0,0,0,1,2,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,1,0,1,1,1,1,1,0,2,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,2,1,0,1,0,2,1,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,2,0,1,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}); new alieni(10,8); new umani(10,9); (int i=0; < 23; i++){ (int j=0; j<14; j++){ if (getmatrix()[i][j]==1){//this it's highlighted arrayindexoutofboundsexception new sicuro(i,j); } else { if (getmatrix()[i][j]==2){ new pericoloso(i,j); } else { if (getmatrix()[i][j]==3){ new scialuppa(i,j); } } } } } } }
your , j should switched around:
for (int i=0; < 14; i++){ (int j=0; j<23; j++){ if (getmatrix()[i][j]==1){//this it's highlighted arrayindexoutofboundsexception new sicuro(i,j); }
i rows < 14 , j columns < 23 need change for
loops...
the first [] row , second [] column [14][x]
[15][x]
[16][x]
etc... cause index out of bounds exception.
Comments
Post a Comment