【问题标题】:how to convert 2d array into 1d?如何将二维数组转换为一维?
【发布时间】:2015-02-03 17:44:28
【问题描述】:

我有一个使用用户输入创建二维数组的代码,它工作正常,但现在我有 2 个问题

第一个:如何将二维数组转换为一维数组?

第二个问题:如何选择或追踪二维数组中右对角线以上的元素?

谁能帮我修复这个代码?

这是我的代码

package question3;

import java.util.Arrays;
import java.util.Collection;
import java.util.Scanner;

public class Array2d {

    public static void main(String[] args) {
        int[][] matrix = new int[3][3];

        int[] array = new int[matrix.length * matrix.length];

        Scanner sc = new Scanner(System.in);

        System.out.print("Please enter 9 integers separated by spaces:");
        for (int i = 0; i < matrix.length; i++) {

            for (int j = 0; j < matrix.length; j++) {
                matrix[i][j] = sc.nextInt();
            }

        }

        int idx = 0;

        for (int row = 0; row < matrix.length; row++) {
            for (int column = 0; column < matrix.length; column++) {
                System.out.print(matrix[row][column] + " "); // Outputs the // array in a // 5x5 grid.


            }
            System.out.println();
        }

        for (int column = 0; column < matrix.length; column++) {
            for (int row = column + 1; row < matrix.length+column ; row++){
                // populate your array here
                array[idx] = matrix[row][column];
                // increment index
                idx++;

                System.out.println(matrix[row][column]);
            }

        }       

    }
}

输出

请输入 9 个以空格分隔的整数: 1 2 3 4 5 6 7 8 9

1 2 3

4 5 6

7 8 9

4 7 8

但我期望 2 , 3 , 6

我需要做的改变是因为我被卡住了,我知道那是在第三个 for 循环中

【问题讨论】:

  • 您能否定义一些您想要转换为一维数组的示例二维数组,并展示您希望输出的样子的示例。
  • 不,先生,这是我的第一篇文章
  • @bhspencer 我将编辑我的问题并显示输出和我想要显示的内容
  • 输入由空格分隔,为什么是 NewLineCharacter ? ,他们不应该与空格在同一行..!

标签: java arrays for-loop multidimensional-array


【解决方案1】:

如果你运行下面的代码,

public class Main {
    public static void main(String[] args) {
        int[][] matrix = new int[5][6];
        System.out.println(matrix.length);
        int[] matrix2 = matrix[4];
        System.out.println(matrix2.length);
    }
}

你会看到它打印出来

5
6

所以最初您有一个长度为5 的数组,其中包含5 个int[],每个长度为˛6

因此它以模式存储

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30

那么你想怎么做才能把这些放到一个数组中呢?你需要从左上到右,然后每次下一行。

    int newArray[] = new int[matrix.length*matrix[0].length];
    for(int i = 0; i < matrix.length; i++) {
        int[] row = matrix[i];
        for(int j = 0; j < row.length; j++) {
            int number = matrix[i][j];
            newArray[i*row.length+j] = number;
        }
    }

这应该可行。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 

完整代码自行查看:

public class Main {
    public static void main(String[] args) {
        int[][] matrix = new int[5][6];

        int counter = 1;
        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 6; j++) {
                matrix[i][j] = counter++;
            }
        }

        int newArray[] = new int[matrix.length*matrix[0].length];
        for(int i = 0; i < matrix.length; i++) {
            int[] row = matrix[i];
            for(int j = 0; j < row.length; j++) {
                int number = matrix[i][j];
                newArray[i*row.length+j] = number;
            }
        }
        for(int i = 0; i < newArray.length; i++) {
            System.out.print(newArray[i] + " ");
        }
    }
}

【讨论】:

  • 你能解释一下你的代码吗,因为我没看懂
  • 你抓取二维矩阵,遍历每个元素并将其复制到新数组中。
  • 如果您可以看到我的编辑问题,我将编辑代码和输出,以向您显示我卡在哪里以及您是否可以帮助我?
【解决方案2】:

matrix.lengtharray.length 都将返回 3 ,因此您的一维数组的大小为 3 而您总共有 9 个元素

所以你不能使用这个,现在如果你的 2D 数组是一个方阵,那么你必须创建 1DArray of size

  int[] array = new int[matrix.length * matrix.length];

然后像@Prashant建议的那样简单地遍历二维数组并将每个元素插入到一维数组中并稍作修改

int k = 0;
for (int i = 0; i < matrix.length; i++) {
    for (int j = 0; j < matrix[i].length; j++) {
        array[k++] = matrix[i][j];
    }
}

【讨论】:

    【解决方案3】:

    使用循环并将一维数组分配为:

     int[] array = new int[matrix.length * matrix[0].length];
    
        int k = 0;
    
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++) {
                array[k++] = matrix[i][j];
            }
        }
    

    【讨论】:

    【解决方案4】:

    您已定义数组大小等于矩阵的行数。它需要有行*列元素。以您认为合适的顺序遍历矩阵时填充数组。这是一个例子:

    package question3;
    
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Array2d {
    
        public static void main(String[] args) {
            int[][] matrix = new int[3][3];
    
            // array must have row * col elements to hold the entire matrix
            int[] array = new int[matrix.length * matrix[0].length];
    
            Scanner sc = new Scanner(System.in);
    
            System.out.println("Please enter 9 integers separated by spaces:");
            for (int i = 0; i < matrix.length; i++) {
    
                for (int j = 0; j < matrix.length; j++) {
                    matrix[i][j] = sc.nextInt();
                }
    
            }
    
            // Index to step through array
            int idx = 0;
    
            for (int row = 0; row < matrix.length; row++) {
                for (int column = 0; column < matrix.length; column++) {
                    System.out.print(matrix[row][column] + " ");
    
                    // populate your array here
                    array[idx] = matrix[row][column];
                    // increment index
                    idx++;
                }
                System.out.println();
            }
    
            System.out.println("the Matrix becomes " + Arrays.toString(array));
    
    
            for (int row = 0; row < matrix.length; row++) {
                for (int column = 0; column < matrix[row].length - row - 1; column++) {
                    // Work with matrix above right diagonal here, matrix[row][column]; 
                    System.out.println(matrix[row][column]);
                }
            }
        }
    }
    

    二维数组的第一个索引是行,第二个是列。您在第二个 for 循环中将它们命名错误。

    要访问从左下角到右上角的对角线上方的所有内容(不包括对角线),请遍历每一行,然后遍历每一列,直到但不包括行长 - 行索引。像这样的:

    for (int row = 0; row < matrix.length; row++) {
        for (int column = 0; column < matrix[row].length - row - 1; column++) {
            // Work with matrix above right diagonal here, matrix[row][column]; 
            System.out.println(matrix[row][column]);
        }
    }
    

    【讨论】:

    • @ Sean Kuhlman 我尝试了对角元素的解决方案,但它没有显示我想要的内容,你能帮我矩阵变成 [1, 4, 7, 5, 8, 9, 0, 0 , 0] 我只想显示 1, 2, 4
    • 查看按预期输出 1、2、3、5、6、9 的编辑。你在重用array吗?如果您需要将值存储在右对角线上方,您可能需要创建一个包含 6 个元素的第三个数组。
    • 我在您更新解决方案后尝试过,但仍然无法正常工作,因为它应该是一维数组的输出 1 2 3 5 6 9 我期望的 1、2、4
    • 当你说右对角线时,我假设主对角线是左上角到右下角。您指的是另一个对角线,并且您希望排除对角线。
    • 哦,对不起,我的意思是从右上角到左下角的误解,但如果我在另一个对角线上工作,输出将为 2,3,6
    【解决方案5】:

    使用 Kotlin,您可以做到;

    fun Array<IntArray>.toVector(): IntArray {
      if (this.isEmpty()) return IntArray(0)
      return this.reduce { firstArr, nextArr ->
        firstArr.plus(nextArr)
      }
    }
    

    这将返回一个包含二维数组所有行的 int 数组。

    【讨论】:

      猜你喜欢
      • 2011-07-05
      • 2015-07-16
      • 1970-01-01
      • 2020-03-26
      • 2016-02-18
      • 2018-02-19
      相关资源
      最近更新 更多