代码中的注释是我试图让它做的,将数组从“12345,12345,12345更改为111,222,333,444,555”
public void reorganize()
{
for(int y = 0; y < repeat; y++)//this loop reorganizes the arrays. Each array is now organized by number location rather than how it was inputted. Ex: 12345, 12345, 12345 is now 111,222,333,444,555
{
for(int r = 0; y==4; r++)
{
newNumbers[r][y] = numbers[y][r];
}
}
我一直在努力解决这个问题,并且我已经重写了几次,但它永远不会最终正常工作。这是我第一次在SO上发帖,我希望你们能帮助我(:
谢谢
答案 0 :(得分:0)
这样的事情:
Integer[][] numbers = new Integer[5][3];
// Fill numbers
Integer[][] newNumbers = new Integer[3][5];
for(int y = 0; y < numbers.length; y++)//this loop reorganizes the arrays. Each array is now organized by number location rather than how it was inputted. Ex: 12345, 12345, 12345 is now 111,222,333,444,555
{
for(int r = 0; r < numbers[0].length; r++)
{
newNumbers[r][y] = numbers[y][r];
}
}
答案 1 :(得分:0)
我建议您首先传递numbers
数组并返回newNumbers
数组。基本上,您的逻辑应该旋转输入numbers
数组的维度以构造newNumbers
,但是您的复制逻辑是正确的(一旦您修复了循环测试条件)。
public static int[][] reorganize(int[][] numbers) {
int[][] newNumbers = new int[numbers[0].length][numbers.length];
for (int y = 0; y < numbers.length; y++) {
for (int r = 0; r < numbers[y].length; r++) {
newNumbers[r][y] = numbers[y][r];
}
}
return newNumbers;
}
然后你应该能够像
那样进行测试public static void main(String[] args) {
int[][] numbers = { { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 },
{ 1, 2, 3, 4, 5 } };
int[][] newNumbers = reorganize(numbers);
System.out.println(Arrays.deepToString(newNumbers));
}
输出(按要求)
[[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5]]