我有一个名为input
的2d数组,我试图在另一个数组中映射不同的数字来更改它。在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]
的结果。
发布于 2017-01-11 09:36:57
你的问题在于这一行:
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;
}
}
https://stackoverflow.com/questions/41596756
复制相似问题