java 如何将二维数组转换为一维数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28305359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert 2d array into 1d?
提问by ttt ttt
i have a code that create 2d array using user input and it work fine but now i have 2 questions
我有一个使用用户输入创建二维数组的代码,它工作正常,但现在我有 2 个问题
the first one: how to convert 2d array to 1d array?
第一个:如何将二维数组转换为一维数组?
second question: how to choose or trace the elements above the right diagonal in the 2d array?
第二个问题:如何选择或追踪二维数组中右对角线上方的元素?
anyone can help me to fix this code?
任何人都可以帮助我修复此代码?
this my code
这是我的代码
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]);
}
}
}
}
output
输出
Please enter 9 integers separated by spaces: 1 2 3 4 5 6 7 8 9
请输入 9 个以空格分隔的整数:1 2 3 4 5 6 7 8 9
1 2 3
1 2 3
4 5 6
4 5 6
7 8 9
7 8 9
4 7 8
4 7 8
but what i expect 2 , 3 , 6
但我期望的是 2 , 3 , 6
where the change that i need to make because i am stuck and i know that is in the third for loop
我需要进行的更改,因为我被卡住了,我知道这是在第三个 for 循环中
回答by EpicPandaForce
Well if you run the following code,
好吧,如果你运行下面的代码,
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);
}
}
You will see that it prints out
你会看到它打印出来
5
6
So initially you have an array that has a length of 5
, and there contains 5 int[]
that have a length of ?6
each.
因此,最初您有一个长度为 的数组,5
其中包含 5int[]
个长度为 ? 6
每个。
Therefore it is stored in the pattern of
因此它存储在模式中
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
So what do you want to do in order to put these into an array? You need to go from topleft to right, and then down a row each time.
那么你想怎么做才能把这些放到一个数组中呢?您需要从左上角到右下角,然后每次向下一行。
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;
}
}
And that should work.
这应该有效。
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
Complete code to see for yourself:
完整代码供您自己查看:
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] + " ");
}
}
}
回答by Prashant
use loop and assign 1-d array as:
使用循环并将一维数组分配为:
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];
}
}
回答by Neeraj Jain
matrix.length
and array.length
both will return 3 , So your 1D array will be of size 3 whereas you have total of 9 elements
matrix.length
并且array.length
两者都将返回 3 ,因此您的一维数组的大小为 3 而您总共有9 个元素
So you cannot use this , Now if your 2D array is a square matrix then you have to create 1 DArray of size
所以你不能使用这个,现在如果你的二维数组是一个方阵,那么你必须创建大小为一维的数组
int[] array = new int[matrix.length * matrix.length];
Then Simply traverse the 2 D Array and insert each element in 1 D Arrayas @Prashant suggested with a little bit of modification
然后简单地遍历二维数组并将每个元素插入一维数组中,正如@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];
}
}
回答by Sean Kuhlman
You have defined the array size equal to the number of rows of the matrix. It needs to have row * column elements. Populate the array as you traverse the matrix in the order you see fit. This is an example:
您已将数组大小定义为等于矩阵的行数。它需要有 row * column 元素。在按照您认为合适的顺序遍历矩阵时填充数组。这是一个例子:
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]);
}
}
}
}
The first index of a two-dimensional array is the row, the second is the column. You have them named incorrectly in the second for loop.
二维数组的第一个索引是行,第二个是列。您在第二个 for 循环中错误地命名了它们。
To access the everything above the diagonal that runs from bottom-left to top-right, excluding the diagonal, step through each row and then each column up to but not including row length - row index. Something like this:
要访问从左下角到右上角的对角线上方的所有内容(不包括对角线),请逐步浏览每一行然后每列直到但不包括行长度 - 行索引。像这样的东西:
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]);
}
}
回答by bebe
With Kotlin, you could just do;
使用 Kotlin,你可以做到;
fun Array<IntArray>.toVector(): IntArray {
if (this.isEmpty()) return IntArray(0)
return this.reduce { firstArr, nextArr ->
firstArr.plus(nextArr)
}
}
This would return an int array comprising of all rows of the 2D array.
这将返回一个包含二维数组所有行的 int 数组。