Reputation: 5
I have a query which generates the following result set
rs = [51,88,93,89,91,26,51,47,47,31,67,68,46,92,39]
my matrix size is 5X5 and I want the final result as upper triangular matrix
51 88 93 89 91
0 26 51 47 47
0 0 31 67 68
0 0 0 46 92
0 0 0 0 39
My code : But does not generate the desired O/P
int rowCount = 5;
String[][] result = new String[rowCount][];
for (int i = 0; i < rowCount; i++) {
Cell[] row = sheet.getRow(i);
result[i] = new String[row.length];
for (int j = 0; j < row.length; j++) {
if(i<=j){
result[i][j] = row[j].getContents();
System.out.print(result[i][j] + " ");
}else{
result[i][j] = "0";
System.out.print(result[i][j] + " ");
}
}
System.out.print("\n");
}
Can you please help what to change in the code to get the proper matrix.
Upvotes: 0
Views: 3087
Reputation: 414
0's are in the cells where column number is lesser then row number.
int rowCount = 5;
int colCount = rowCount;
int[][] result = new int[rowCount][colCount];
int[] input = { 51, 88, 93, 89, 91, 26, 51, 47, 47, 31, 67, 68, 46, 92, 39};
int k = 0;
for (int row = 0; row < rowCount; row++) {
for (int column = 0; column < colCount; column++) {
result[row][column] = row > column ? 0 : input[k++];
}
}
Upvotes: 1