gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:01:57 27 4
gpt4 key购买 nike

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

 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;
}

如果这仍然不清楚,我的目标是将输入

更改
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]得到的结果。

最佳答案

您的问题出在这一行:

 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;
}
}

关于java - 将数组值映射到另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41596756/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com