无法实例化Switch语句内的字符串数组-Java

无法实例化Switch语句内的字符串数组-Java,java,arrays,string,switch-statement,instantiation,Java,Arrays,String,Switch Statement,Instantiation,由于某种原因,字符串[]champs没有被实例化,我似乎不明白为什么 不应该champs=newstring[]{“诸如此类”、“诸如此类”、“诸如此类”}初始化数组 public static String[] getChamps() { String rolereturn = ChampSelect.getRoles(); //Switch to Determine Champion Suggestions String[] champs; //Strin

由于某种原因,字符串[]champs没有被实例化,我似乎不明白为什么
不应该
champs=newstring[]{“诸如此类”、“诸如此类”、“诸如此类”}初始化数组

  public static String[] getChamps() {

    String rolereturn = ChampSelect.getRoles();

    //Switch to Determine Champion Suggestions


    String[] champs; //String Declaration

    switch (rolereturn) {

        case "AD Carry": //AD Carry Selection Options
            champs = new String[]{ "Ashe", "Caityln", "Draven", "Ezreal", "Kog'Maw", "Sivir", "Twitch", "Varus", "Vayne" };
            break;

        case "AP Carry": //AP Carry Selection Options
            champs = new String[]{ "Diana", "Evelyn", "Kassadin", "Kennen" };
            break;

        case "Support": //Support Selection Options
            champs = new String[]{ "Janna", "Nunu", "Shen", "Soraka", "Taric", };
            break;

        case "AP Jungle": //AP Jungle Selection Options
            champs = new String[]{ "Diana", "Fiddlesticks" };
            break;

        case "AD Jungle": //AD Jungle Selection Options
            champs = new String[]{ "Kha'Zix", "Nocturne", "Rengar", "Udyr", "Warwick", };
            break;

        case "AP Top": //AP Top Selection Options
            champs = new String[]{ "Akali", "Cho'Gath", "Kennen", "Malphite", "Shen", "Singed", "Teemo" };
            break;

        case "AD Top": //AD Top Selection Options
            champs = new String[]{ "Fiora", "Irelia", "Jax", "Kha'Zix", "Master Yi", "Nasus", "Nidalee", "Rengar", "Zed" };
            break;
    }
    return champs;
}

您是对的,如果调用其中一个case语句,它将初始化数组。我会在其中添加一个
default
,然后在调用
default
时抛出。当没有匹配的case语句时,将调用默认值。看起来您涵盖了所有的case语句,因此使用
default
throw将是一个好主意

像这样的东西应该可以

 default: throw new RuntimeException("SHould not be here " + rolereturn);

您是对的,如果调用其中一个case语句,它将初始化数组。我会在其中添加一个
default
,然后在调用
default
时抛出。当没有匹配的case语句时,将调用默认值。看起来您涵盖了所有的case语句,因此使用
default
throw将是一个好主意

像这样的东西应该可以

 default: throw new RuntimeException("SHould not be here " + rolereturn);

最可能的原因是
rolereturns
与您的想法不符。您能否尝试在交换机的末尾添加一个默认值,将数组设置为某个值,然后看看是否可以这样做


否则,只需调试并检查是否进入正确的行…

最可能的原因是
rolereturns
与您的想法不符。您能否尝试在交换机的末尾添加一个默认值,将数组设置为某个值,然后看看是否可以这样做


否则,只需调试并检查是否进入正确的行…

我会使用
映射而不是开关盒

public static String[] getChamps() {

    Map<String,String[]> map = new HashMap();
    map.put("AD Carry",new String[]{ "Ashe", "Caityln", "Draven", "Ezreal", "Kog'Maw", "Sivir", "Twitch", "Varus", "Vayne" });
    // ... and so on for all your cases

    String rolereturn = ChampSelect.getRoles();
    if (!map.containsKey(rolereturn)) throw new IllegalArgumentException(rolereturn);

    return map.get()    
}
public静态字符串[]getChamps(){
Map Map=newhashmap();
map.put(“AD Carry”,新字符串[]{“Ashe”,“Caityln”,“Draven”,“Ezreal”,“Kog'Maw”,“Sivir”,“Twitch”,“Varus”,“Vayne”});
//…等等你所有的案子
字符串rolereturn=ChampSelect.getRoles();
如果(!map.containsKey(rolereturn))抛出新的IllegalArgumentException(rolereturn);
returnmap.get()
}

我会使用
地图而不是开关盒

public static String[] getChamps() {

    Map<String,String[]> map = new HashMap();
    map.put("AD Carry",new String[]{ "Ashe", "Caityln", "Draven", "Ezreal", "Kog'Maw", "Sivir", "Twitch", "Varus", "Vayne" });
    // ... and so on for all your cases

    String rolereturn = ChampSelect.getRoles();
    if (!map.containsKey(rolereturn)) throw new IllegalArgumentException(rolereturn);

    return map.get()    
}
public静态字符串[]getChamps(){
Map Map=newhashmap();
map.put(“AD Carry”,新字符串[]{“Ashe”,“Caityln”,“Draven”,“Ezreal”,“Kog'Maw”,“Sivir”,“Twitch”,“Varus”,“Vayne”});
//…等等你所有的案子
字符串rolereturn=ChampSelect.getRoles();
如果(!map.containsKey(rolereturn))抛出新的IllegalArgumentException(rolereturn);
returnmap.get()
}

您是否尝试使用调试器运行此方法以检查是否执行了其中一条case语句?顺便说一句:当你有一个switch语句时,你应该总是有一个
默认值
的case,它做了一些有意义的事情或者抛出了一个异常。虽然你的case应该可以工作,但是你也可以从
champs=newstring[]
中删除这个
[]
。只要试着使用
String[]champs={“x”、“y”、“z”}……
+1@Philipp@bonCodigo如果在每个case中执行此操作,则在切换后将无法再访问数组。是否尝试使用调试器运行此方法以检查是否执行了其中一个case语句?顺便说一句:当你有一个switch语句时,你应该总是有一个
默认值
的case,它做了一些有意义的事情或者抛出了一个异常。虽然你的case应该可以工作,但是你也可以从
champs=newstring[]
中删除这个
[]
。只要试着使用
String[]champs={“x”、“y”、“z”}……
+1@Philipp@bonCodigo如果您在每个案例中都这样做,那么在切换之后阵列将不再可访问。谢谢您,正是这样,我以前没有使用过switch语句,也没有想过在其中添加默认值,现在就完成其余的操作@SeanPatrickDill这是一件好事,任何人都不应该使用switch语句,曾经,请参阅我的答案以获得更干净的解决方案。谢谢你,正是这样,我以前从未使用过switch语句,也没有想过要使用默认值,现在来完成剩下的@SeanPatrickDill这是一件好事,任何人都不应该使用switch语句,永远,看看我的答案,寻找更干净的解决方案。