java - array not working properly -
hi im trying count frequency of 2d array . trying display frequency in way example if table :
0: 1 2 0
1: 2 0 1
2: 1 0 2
i want able count frequency :
0: 0 2 1
1: 2 0 1
2: 1 1 1
so way table should how many times 0 has appeared in first column , how many times 1 has appears in first column , on. not sure problem have . notice 1 gets 2nd iteration stops working or gives out 0
the code have far is
(int col =0; col< s ; col++){ system.out.print(col+ ": "); (int row = 0; row<s; row++) { x=val[row][col]; if (table[row][col]==row) { system.out.print(x++ + " "); } //system.out.print(val[col][row]+" "); if (row+1==s) system.out.println(); } } }
thanks
assuming regular shaped array. make more robust want detect maximum number of columns.
int[][] table = new int[][]{{1,2,0},{2,0,1},{1,0,2}}; int[][] result = new int[table.length][table.length]; (int row = 0; row<result.length;row++){ arrays.fill(result[row], 0); } // iterating through multi dimensional arrays easier start // dimension 0 (int row = 0; row<table.length;row++){ (int col =0; col< table[row].length ; col++){ // row in result table equals value of cell. int index = table[row][col]; result[index][col]++; } } system.out.println(arrays.deeptostring(result));
Comments
Post a Comment