java - Cut an 2d Array down to needed part -


i have array numbers in it, procedurally generated through code , want remove part of array not necessary.

so until tried best create own code, didn't work quite (thats reason why im here).

this try:

private void cutplacementmap() {     int firstneededx = integer.max_value;     int firstneededz = integer.max_value;     int lastneededx = -1;     int lastneededz = -1;      (int x = 0; x < placementmap.length; x++) {         (int z = 0; z < placementmap[0].length; z++) {             if (placementmap[x][z] != 0) {                 if (x < firstneededx)                     firstneededx = x;                  if (z < firstneededz)                     firstneededz = z;                  if (x > lastneededx)                     lastneededx = x;                  if (z > lastneededz)                     lastneededz = z;             }         }     }      int lengthx = lastneededx - firstneededx;     int lengthz = lastneededz - firstneededz;     int[][] newplacementmap = new int[lengthx + 1][lengthz + 1];      system.out.println("lengthx: " + lengthx);     system.out.println("lengthz: " + lengthz);     int conx = 0;     int conz = 0;      (int x = firstneededx; x <= lastneededx; x++) {         (int z = firstneededz; x <= lastneededz; z++) {             newplacementmap[conx][conz] = placementmap[x][z];             conz++;         }         conz = 0;         conx++;     }      placementmap = newplacementmap;  } 

this code throws arrayindexoutofboundexception. pointing line newplacementmap[conx][conz] = placementmap[x][z]; reason exception.

every appreciated. thanks.

debugging should pretty easy.

take @ loop:

for (int z = firstneededz; x <= lastneededz; z++) 

this loop forever (or until arrayindexoutofboundsexception), because stop condition contains typo.

x <= lastneededz must of course z <= lastneededz.


Comments