3

I am trying to see if the multidimensional array is rectangular or not. I am new to programming and can't exactly figure out why the "break;" will not kick me out of the loop and it continues to run. Even with the array not being rectangular, I still get back true.

public static void main(String[] args) {

    int a2d[][] = {{1, 2, 3, 4, 5}, {2, 3, 4}, {1, 2, 3, 4, 5}};

    int test = a2d[0].length;

    for (int i = 0; i < a2d.length; i++) {
        for (int j = 0; j < a2d[i].length; j++) {
            if (a2d[i].length == test) {
                System.out.println("True");
            } else {
                System.out.println("False");
                break;
            }
        }
    }
}
Tiny
  • 27,221
  • 105
  • 339
  • 599
ZWis212
  • 132
  • 1
  • 13

3 Answers3

3

In order to avoid labels, put your code into a method that returns a boolean:

boolean isRectangular(int[][] a2d) {
    int test = a2d[0].length;
    for (int i=0; i<a2d.length; i++){
        for (int j=0; j<a2d[i].length; j++){
            if (a2d[i].length != test) {
                return false;
            }
        }
    }
    return true;
}

The code can be improved to support arguments checks and whatnot, but the point is you return from the method as soon as you determine your answer.

bdkosher
  • 5,753
  • 2
  • 33
  • 40
2

A Java 8 approach of the problem would be along the lines of:

int a2d[][] = {{1, 2, 3, 4, 5}, {2, 3, 4}, {1, 2, 3, 4, 5}};
boolean isRectangular = 
    Arrays.stream(a2d) // First, create a stream
            .map(row -> row.length) // Map the length of each row to process further
            .allMatch(len -> len == a2d[0].length); // Verify the length of all rows

If used, no external loop is required which means no break. Furthermore, the loop can be parallel if needed (to possibly speed up things).

wassgren
  • 18,651
  • 6
  • 63
  • 77
  • 1
    because that is a lot clearer ;) – Joeblade Dec 22 '14 at 17:39
  • @Joeblade, I agree. Quite a lot clearer (at least if you are used to Java 8 syntax). But, the main upside is that you don't need to loop yourself. – wassgren Dec 22 '14 at 17:41
  • Haha ^^^ I honestly do not know exactly how that works but I will hopefully understand that sooner or later. I'm guessing that's all apart of the Arrays class. – ZWis212 Dec 22 '14 at 17:42
  • Well, it is quite straightforward once you are used to Streams and Lambdas. I updated the code with some comments. First, you create a stream (`Arrays.stream`). That stream contains a series of `int[]` objects that you `map` to contain the length. Last, match all lengths i.e. check that all lengths are the same. – wassgren Dec 22 '14 at 17:45
  • I'm weeping in anticipation at new developers coming in and putting code like that into my project, and then a year later someone has to add another operation to it. – Joeblade Dec 22 '14 at 17:45
  • Hear hear @Joeblade ;) But on a more serious note, a nested for-loop with an if-statement and a multi-dimensional array is a dream to work with ;) – wassgren Dec 22 '14 at 17:47
  • @wassgren hehe yeah that's true. I'm just being a bit dramatic and to be fair I am being a bit of a luddite, i'll have to get used to the new way of doing things. I'm just worried about juniors (since java8 is likely to be taught just after they've had their unit of functional programming in university) using this at every turn. And there are disadvantages to this way of coding. Exception handling is a major one. But for cases where you don't expect refactoring / exceptions this can reduce boilerplate coding. – Joeblade Dec 23 '14 at 08:05
0
public static void main(String[] args) {

        int a2d[][] = {{1,2,3,4,5},
                       {2,3,4},
                       {1,2,3,4,5}};

        int test = a2d[0].length;
        outer:
        for (int i=0; i<a2d.length; i++){

            for (int j=0; j<a2d[i].length; j++){

                if (a2d[i].length == test) {
                    System.out.println("True");
                } else {
                    System.out.println("False");
                    break outer;
                }
            }
        }
    }

This should do. Google out on labeled for loops. This should help you as well.

Community
  • 1
  • 1
sakthisundar
  • 3,278
  • 3
  • 16
  • 29
  • I guess I never really thought about only breaking out of the inside loop... Now it makes perfect sense why it wasn't false. Thanks all. – ZWis212 Dec 22 '14 at 17:38