将数组值映射到另一个数组

时间:2017-01-11 17:11:08

标签: java arrays mapping

我有这个名为input的二维数组,我试图在另一个数组中映射不同的数字来改变它。在2d数组mapping中,它包含我想要执行的所有映射。第一个数组是{0,2,3,4,1,5},我想用它来更改input中的值,所以每1个变为2 ,2到3和3到4和4到1.我在该映射数组中有23个不同的映射,我想要更改input的值。这是我到目前为止所尝试的。

 public static int [][] mapOrientation(int input[][]){
    int [][] input = {{0,0,0,5},{4,1,1,4},{2,2,0,2},{1,2,3,5},{3,3,3,4},{1,5,5,4}};

     int[][] mapping ={{0,2,3,4,1,5},{1,2,0,4,5,3},{5,2,1,4,3,0},{3,2,5,4,0,1},
                       {3,5,4,0,2,1},{2,5,3,0,1,4},{1,5,2,0,4,3},{4,5,1,0,3,2},
                       {4,1,0,3,5,2},{5,1,4,3,2,0},{2,1,5,3,0,4},{0,1,2,3,4,5},
                       {0,4,1,2,3,5},{3,4,0,2,5,1},{5,4,3,2,1,0},{1,4,5,2,0,3},
                       {1,0,4,5,2,3},{2,0,1,5,3,4},{3,0,2,5,4,1},{4,0,3,5,1,2},
                       {4,1,0,3,5,2},{5,1,4,3,2,0},{2,1,5,3,0,4}};

    int [] comb = mapping[0];
    for(int i = 0; i < input.length; i++){
        for(int j = 0; j < input[i].length; j++){

            input[i][j] = comb[0];
            System.out.println(comb[0]);
            System.out.println(Arrays.deepToString(input));

   cubeToString = Arrays.deepToString(input);

    return input;
}

如果仍然不清楚,我的目标是从

更改input
int [][] input ={{5,1,2,0},{2,0,1,4},{1,2,5,5},{3,0,1,3},{4,3,0,4},{3,2,5,4}} 

为:

int [][] input ={{5,2,3,0},{3,0,2,1},{2,3,5,5},{4,0,2,4},{1,4,0,1},{4,3,5,1}}

这将是我想要使用comb[0]的结果。

1 个答案:

答案 0 :(得分:1)

你的问题在于这一行:

 input[i][j] = comb[0];

您将值设置为始终等于映射的第一项。你真正想要的是

 input[i][j] = comb[input[i][j]];

可运行的示例

public class NewClass
{
    public static void main(String... args)
    {
        int[][] input = {{5,1,2,0},{2,0,1,4},{1,2,5,5},{3,0,1,3},{4,3,0,4},{3,2,5,4}};
        mapOrientation(input);
    }

    public static int [][] mapOrientation(int input[][])
    {
        int[][] mappings ={{0,2,3,4,1,5},{1,2,0,4,5,3},{5,2,1,4,3,0},{3,2,5,4,0,1},
                           {3,5,4,0,2,1},{2,5,3,0,1,4},{1,5,2,0,4,3},{4,5,1,0,3,2},
                           {4,1,0,3,5,2},{5,1,4,3,2,0},{2,1,5,3,0,4},{0,1,2,3,4,5},
                           {0,4,1,2,3,5},{3,4,0,2,5,1},{5,4,3,2,1,0},{1,4,5,2,0,3},
                           {1,0,4,5,2,3},{2,0,1,5,3,4},{3,0,2,5,4,1},{4,0,3,5,1,2},
                           {4,1,0,3,5,2},{5,1,4,3,2,0},{2,1,5,3,0,4}};

        System.out.println("Before");
        System.out.println(Arrays.deepToString(input));

        int[] mapping = mappings[0];
        for(int i = 0; i < input.length; i++)
        {
            for(int j = 0; j < input[i].length; j++)
            {
                input[i][j] = mapping[input[i][j]];
            }
        }

        System.out.println("After");
        System.out.println(Arrays.deepToString(input));

        //cubeToString = Arrays.deepToString(input);

        return input;
    }
}