If-else 切换到 Java
If-else to Switch in Java
我有这个 if-else
语句,它接受一个二维数组作为参数并检查它是 null
还是空的,我试图将它转换为 switch
语句,因为我想插入 break
行。
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
}
}
我尝试执行 switch(matrix)
,但 Eclipse 显示一条消息说它无法打开 int[][]
类型的值。
没错。
Java 中的 switch
不执行 "complex" 对象(或奇特的条件和模式匹配),只是基元、枚举和字符串。
你必须坚持你的 if
声明(无论如何也不算太糟糕)。
我有这个 if-else
语句,它接受一个二维数组作为参数并检查它是 null
还是空的,我试图将它转换为 switch
语句,因为我想插入 break
行。
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
}
}
我尝试执行 switch(matrix)
,但 Eclipse 显示一条消息说它无法打开 int[][]
类型的值。
没错。
Java 中的 switch
不执行 "complex" 对象(或奇特的条件和模式匹配),只是基元、枚举和字符串。
你必须坚持你的 if
声明(无论如何也不算太糟糕)。