arrays - How do I fix the error: java.lang.ArrayIndexOutOfBoundsException: 1? -
this question has answer here:
all want return two-dimensional point array reason keep receiving error message. can me fix please? error message directed toward line of code @ end of first 'if' statement.
btw - method takes board , finds matches of length 3 or greater , returns points in single array. both horizontally , vertically. supposed iterate through each point on board, check this.
public static point[][] findmatches(board b) { int counter = 0; int prev = 2; int x = 0; int y = 0; point n = new point(x,y); point[][] foundmatches = new point[1][0]; //instead of 1? for(y=0; y<b.getsize(); y++) { for(x=0; x<=b.getsize(); x++) { if (buildpossiblematchrow(n,b).length >=3 ) //switch x , y in columns { counter++; if(counter==prev) { foundmatches = expandarray(foundmatches); prev = prev*2; } foundmatches[counter]=buildpossiblematchrow(n,b); } if (buildpossiblematchcolumn(n,b).length >=3 ) { counter++; if(counter==prev+1) { foundmatches = expandarray(foundmatches); prev = prev*2; } foundmatches[counter]=buildpossiblematchcolumn(n,b); } } } return foundmatches;
}
here requested method code:
public static point[] buildpossiblematchrow(point p, board b) { int x = p.x; int y = p.y; boolean[] matches = b.getrowbools(new point(x,y)); int count = 0; int currindex = 0; (int a=0; < matches.length; a++) { if(matches[a] && count==0) { count++; } else if(matches[a] && matches[a-1] && count!=0) { count++; } else { count+=0; } } point[] arrayofpossiblematches = new point[count]; (int i=0; i<count; i++) { arrayofpossiblematches[currindex] = new point((x+i), y); currindex++; } return arrayofpossiblematches; }
expand array --->
public static point[] expandarray(point[] originalarray) { int increase = originalarray.length; point[] doublearray = new point[increase*2]; for(int i=0; i<originalarray.length; i++){ doublearray[i]=originalarray[i]; } return doublearray; }
current problem:
using following board
1 1 2 1
2 3 1 3
3 2 3 1
3 1 1 3
testing findmatches passed
swapping , testing
1 1 1 1
2 3 2 3
3 2 3 1
3 1 1 3
testing findmatches
failed
correct output: [[(0,0), (1,0), (2,0), (3,0)]]
your output: [[(0,0), (1,0), (2,0), (3,0)], [(1,0), (2,0), (3,0), (4,0)], [(2,0), (3,0), (4,0), (5,0)], [(3,0), (4,0), (5,0), (6,0)]]
try code out , let me know results.
public static point[][] findmatches(board b) { // int counter = 0; // int prev = 2; int x = 0; int y = 0; point n; // use arraylist dynamic memory allocation arraylist<point[]> foundmatches = new arraylist<point[]>(); //instead of 1? for(y=0; y<b.getsize(); y++) { for(x=0; x<b.getsize(); x++) { n = new point(x, y); // n staying @ (0,0) previous code /* * create tmp variables have call accessors * once */ point[] tmppossiblematchrow = buildpossiblematchrow(n,b); point[] tmppossiblematchcolumn = buildpossiblematchcolumn(n,b); if (tmppossiblematchrow.length >=3 ) //switch x , y in columns { foundmatches.add(tmppossiblematchrow); } if (tmppossiblematchcolumn.length >=3 ) { foundmatches.add(tmppossiblematchcolumn); } } } point[][] matches = new point[foundmatches.size()][]; for(int = 0; < foundmatches.size(); i++) { matches[i] = foundmatches.get(i); } return matches; }
Comments
Post a Comment