I have sa 2-dimensional array with lots of rows and columns, with random numbers between 0 and 255. I'm trying to look for instances of particular integers within my array, ie those between 231 and 255, and simply print out a String, ie "/", "." or a space, each time it comes across such integers. I suppose the following code only works for columns. How might I extend this into rows?
int[][] result = function(parameter);
System.out.println(Arrays.deepToString(result));
for (int i = 1; i <= result.length-1; i++) {
if (result[i][i] >= 179 && result[i][i] <= 204) {
System.out.print("\\");
}
if (result[i][i] >= 205 && result[i][i] <= 230) {
System.out.print(".");
}
if (result[i][i] >= 231 && result[i][i] <= 255) {
System.out.print(" ");
}
}
You code is only testing elements on the diagonal of the 2d array. You should have a nested loop in order to loop over the entire array :
for (int i = 0; i <= result.length-1; i++) {
for (int j = 0; j <= result[i].length-1; j++) {
if (result[i][j] >= 179 && result[i][j] <= 204) {
System.out.print("\\");
}
if (result[i][j] >= 205 && result[i][j] <= 230) {
System.out.print(".");
}
if (result[i][j] >= 231 && result[i][j] <= 255) {
System.out.print(" ");
}
}
System.out.println("");
}
You can simply traverse the rows as well, If I understood what you want correctly
int[][] result = function(parameter);
System.out.println(Arrays.deepToString(result));
for (int row = 0; row < result.length; row++) {
for (int col= 0; col< result[0].length; col++) {
if (result[row][col] >= 179 && result[row][col] <= 204) {
System.out.print("\\");
}
if (result[row][col] >= 205 && result[row][col] <= 230) {
System.out.print(".");
}
if (result[row][col] >= 231 && result[row][col] <= 255) {
System.out.print(" ");
}
}
}
you can use the following ways to convert an integer into string
Integer.toString(i)
or String.valueOf(i)
Example:
Integer.toString(result[i][j])
String.valueOf(result[i][j])
anyway your problem is that you need two loops one for rows and 2nd for columns, that is why you are getting only the values of columns :)
int[][] result = function(parameter);
System.out.println(Arrays.deepToString(result));
for (int row = 0; row < result.length; row++) {
for (int col= 0; col< result[0].length; col++) {
if (result[row][col] >= 179 && result[row][col] <= 204) {
System.out.print("\\");
}
if (result[row][col] >= 205 && result[row][col] <= 230) {
System.out.print(".");
}
if (result[row][col] >= 231 && result[row][col] <= 255) {
System.out.print(" ");
}
}
}
If I understand your question, then I believe the easiest approach is to use a StringBuilder
and dynamically build your output String
. Iterate each array in your multidimensional array, and test each value (using else
so that each test doesn't logically exclude the previous tests) like
int[][] result = { { 1, 179 }, { 205, 231 }, { 256 } };
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < result.length; i++) {
int[] arr = result[i];
if (i != 0) {
sb.append(", ");
}
sb.append("[");
for (int j = 0; j < arr.length; j++) {
if (j != 0) {
sb.append(", ");
}
if (arr[j] >= 179 && arr[j] <= 204) {
sb.append("\\");
} else if (arr[j] >= 205 && arr[j] <= 230) {
sb.append(".");
} else if (arr[j] >= 231 && arr[j] <= 255) {
sb.append(" ");
} else {
sb.append(arr[j]);
}
}
sb.append("]");
}
sb.append("]");
System.out.println(sb.toString());
Output is
[[1, \], [., ], [256]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.