0

I have s a 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, i.e. those between 231 and 255, and simply print out a String, i.e. "/", "." 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(" ");
    }
}
2
  • can you show your expected output? Commented Nov 13, 2014 at 7:13
  • to iterate a n dimensional array, you need to have n nested loops. Commented Nov 13, 2014 at 7:13

4 Answers 4

1

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(" ");
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This solves the problem. I began using a nested for-loop but where you have "row < result.length;" I had "row <= result.length;". Likewise for the column integer. This was causing an ArrayIndexOutOfBoundsException. The answer you provided clears this issue up.
1

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("");
}

Comments

1

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]]

1 Comment

This would be very helpful if I was converting the array literals to Strings, but all I wanted to do was print particular symbols as I read through the array. Either way, thank you for the detailed answer, as it is definitely related to a possible extended version of this question :)
1

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(" ");
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.