我有这个 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
不执行“复杂”对象(或花哨的条件和模式匹配),仅执行基元、枚举和字符串。
你必须坚持使用你的 if
语句(无论如何也不算太糟糕)。
关于java - Java 中的 If-else 切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33489834/