Ho provato a modificarlo nel seguente modo:
codice:package lettura_mappa; import java.util.ArrayList; class Coppia { int riga; int colonna; public Coppia(int r, int c) { riga = r; colonna = c; } public int get_riga() { return riga; } public int get_colonna() { return colonna; } public String toString(){ return "(" + riga + "," + colonna + ")"; } } public class Matrix { private Coppia[][] matrix = null; public Matrix() { } public Matrix(ArrayList<ArrayList<Cella_base>> mappa) { int righe = mappa.size(); int colonne = mappa.get(0).size(); matrix = new Coppia[righe][colonne]; for (int i = 0; i < righe; i++) { for (int j = 0; j < colonne; j++) { matrix[i][j] = new Coppia(i, j); } } } public Matrix(Coppia[][] matrix) { this.matrix = matrix; } public Matrix getSubMatrix(int row, int column, int width, int height) { try { int r0 = matrix.length - row > height ? height : matrix.length - row; int c0 = matrix.length - column > width ? width : matrix.length - column; Coppia[][] m = new Coppia[r0][c0]; for (int r = 0; r < m.length; r++) { for (int c = 0; c < m[0].length; c++) { m[r][c] = matrix[row + r][column + c]; } } return new Matrix(m); } catch (Exception e) { return new Matrix(); } } public Coppia getValue(int row, int column) { try { return matrix[row][column]; } catch (Exception e) { return new Coppia(-1, -1); } } /*public void setValue(int row, int column, int value) { try { matrix[row][column] = value; } catch (Exception e) { } }*/ public int getRows() { try { return matrix.length; } catch (Exception e) { return 0; } } public int getColumns() { try { return matrix[0].length; } catch (Exception e) { return 0; } } @Override public String toString() { StringBuffer sb = new StringBuffer(""); try { for (int r = 0; r < matrix.length; r++) { sb.append("["); for (int c = 0; c < matrix[0].length; c++) { sb.append("[" + matrix[r][c] + "]"); } sb.append("]"); } } catch (Exception e) { sb.append("[]"); } return sb.toString(); } public static void main(String[] args) { Mappa_reale map = new Mappa_reale(); System.out.println(map.mappa); Matrix m = new Matrix(map.mappa); int n = 2; int rLimit = (int) Math.ceil((double) m.getRows() / (double) n); int cLimit = (int) Math.ceil((double) m.getColumns() / (double) n); for (int r = 0, rr = 0; r < rLimit; r++, rr += n) { for (int c = 0, cc = 0; c < cLimit; c++, cc += n) { Matrix sm = m.getSubMatrix(rr, cc, n, n); System.out.println(sm); } } } }
Come output ho questo:
considerando che la matrice è questa:codice:[[(0,0)][(0,1)]][[(1,0)][(1,1)]] [] [[(2,0)][(2,1)]][[(3,0)][(3,1)]] [] [[(4,0)][(4,1)]] []
In pratica non ci stampa l'ultima colonna e ci restituisce quindi quelli spazi vuoti [].codice:00 01 02 10 11 12 20 21 22 30 31 32 40 41 42
Forse abbiamo modificato male il getsubmatrix?


Rispondi quotando