在java中的if语句中定义数组

时间:2014-12-17 23:01:49

标签: java arrays switch-statement

我已尝试在switch语句中声明它......

    switch (c)
    {
        case 0: int[] statList = {5,1,3,3,1,2}; break;
        case 1: int[] statList = {3,1,5,2,1,3}; break;
        case 2: int[] statList = {1,5,3,1,3,2}; break;
        case 3: int[] statList = {3,1,2,5,3,1}; break;
        case 4: int[] statList = {3,2,3,1,1,5}; break;
        case 5: int[] statList = {1,3,2,3,5,1}; break;
        case 6: int[] statList = {4,4,1,1,1,4}; break;
        default: int[] statList = {0,0,0,0,0,0};
    }

并在switch语句之外......

    int[] statList = new int[6];
    switch (c)
    {
        case 0: statList = {5,1,3,3,1,2}; break;
        case 1: statList = {3,1,5,2,1,3}; break;
        case 2: statList = {1,5,3,1,3,2}; break;
        case 3: statList = {3,1,2,5,3,1}; break;
        case 4: statList = {3,2,3,1,1,5}; break;
        case 5: statList = {1,3,2,3,5,1}; break;
        case 6: statList = {4,4,1,1,1,4}; break;
        default statList = {0,0,0,0,0,0};
    }

但是两个都给了我一个错误,有没有一个简单的方法可以在不使用循环或逐个定义每个值的情况下执行此操作?因为这首先会破坏我使用数组的目的。

0 个答案:

没有答案