Rods2292
Rods2292

Reputation: 675

If-else to Switch in Java

I have this if-else statement that takes an 2d-array as argument and checks if it's null or empty and I'm trying to convert it to a switch statement because I want to insert a break line.

public static void method (int[][]matrix, int x){
    if (matrix == null){ // Matrix is an int[][]
                System.out.println("It's NULL");
                            // I want to insert a break line here
            } else if (matrix.length == 0){ // Checks if it is empty
                System.out.println("Empty array");
                           // I want to insert a break line here
            } else {
                    // Calculates other things if it's not NULL or empty
                }
}

I tried to do switch(matrix) but Eclipse shows a message saying that it cannot switch on a value of type int[][].

Upvotes: 1

Views: 216

Answers (1)

Thilo
Thilo

Reputation: 262824

That's right.

A switch in Java does not do "complex" objects (or fancy conditions and pattern matches), just primitives, enums, and Strings.

You have to stick with your if statements (which is not too bad anyway).

Upvotes: 3

Related Questions