One of the purest benefits of coding modern collections is the ability to use each design. I have below a simple general method for printing a table, followed by a test load method. While this works, some will be much cleaner for everyone. Any ideas?
public void printTable(Table table) { int numRows = table.rowKeySet().size(); int numCols = table.columnKeySet().size(); for (int i=0; i<numRows; i++) { for (int j=0; j<numCols; j++) { System.out.print( table.get(i,j) + " " ); } System.out.println(); } } Table<Integer, Integer, Integer> table = HashBasedTable.create(); void makeTable() { for (int i=0; i<4; i++) for (int j=0; j<6; j++) table.put(i, j, i*j+2); }
source share