java开关默认用于案例中未使用的其余索引
我需要使用开关的默认值为数组元素分配值,这些元素的索引在案例中没有使用 例如:java开关默认用于案例中未使用的其余索引,java,switch-statement,Java,Switch Statement,我需要使用开关的默认值为数组元素分配值,这些元素的索引在案例中没有使用 例如: public class sample { public static void main(String[] args) { String[] animalArray = new String[5]; String animal = "Dog"; switch(animal){ case "Dog": //The position 0 i
public class sample {
public static void main(String[] args) {
String[] animalArray = new String[5];
String animal = "Dog";
switch(animal){
case "Dog":
//The position 0 is found using some calculation
animalArray[0] = "Dog";
break;
case "Cat":
////The position 3 is found using some calculation
animalArray[3] = "Cat";
break;
default:
//How do I get the value of x to be 1,2,4
animalArray[x] = "Undefined";
}
}
}
现在,我需要一些方法来告诉您,在缺省情况下,应该在这里使用case语句中尚未使用的索引
如何实现这一点?在使用数组之前,我会预先填充数组
String[] animalArray = new String[5];
Arrays.fill(anumalArray, "undefined");
然后您可以设置任何已知值。在使用数组之前,我会预先填充数组
String[] animalArray = new String[5];
Arrays.fill(anumalArray, "undefined");
然后可以设置任何已知值