Java 如何将数组存储在单个数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2566513/
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 store arrays in single array
提问by Jessy
How can I store arrays in single array?
e.g. I have four different arrays, I want to store it in single array int storeAllArray []
and when I call e.g. storeAllArray[1] , I will get this output [11,65,4,3,2,9,7]
instead of single elements?
如何将数组存储在单个数组中?例如,我有四个不同的数组,我想将它存储在单个数组中int storeAllArray []
,当我调用例如 storeAllArray[1] 时,我会得到这个输出[11,65,4,3,2,9,7]
而不是单个元素?
int array1 [] = {1,2,3,4,5,100,200,400};
int array2 [] = {2,6,5,7,2,5,10};
int array3 [] = {11,65,4,3,2,9,7};
int array4 [] = {111,33,22,55,77};
int storeAllArray [] = {array1,array2,array3,array2}
// I want store all array in on array
int storeAllArray [] = {array1,array2,array3,array2}
// 我想将所有数组存储在数组中
for (int i=0; i<storeAllArray; i++){
System.out.println(storeAllArray.get[0]); // e.g. will produce --> 1,2,3,4,5,100,200,400 , how can I do this?
}
EDITED: How can I get output like this?
编辑:我怎样才能得到这样的输出?
System.out.println(storeAllArray [0]) --> [1,2,3,4,5,100,200,400];
System.out.println(storeAllArray [1]) --> [2,6,5,7,2,5,10];
System.out.println(storeAllArray [2]) --> [11,65,4,3,2,9,7];
System.out.println(storeAllArray [2]) --> [111,33,22,55,77];
采纳答案by Péter T?r?k
int array1 [] = {1,2,3,4,5,100,200,400};
int array2 [] = {2,6,5,7,2,5,10};
int array3 [] = {11,65,4,3,2,9,7};
int array4 [] = {111,33,22,55,77};
int[] storeAllArray [] = {array1,array2,array3,array4};
for (int[] array : storeAllArray) {
System.out.println(Arrays.toString(array));
}
In Java 5 and above, this prints
在 Java 5 及更高版本中,这会打印
[1, 2, 3, 4, 5, 100, 200, 400]
[2, 6, 5, 7, 2, 5, 10]
[11, 65, 4, 3, 2, 9, 7]
[111, 33, 22, 55, 77]
Prior to Java 5, you should use
在 Java 5 之前,您应该使用
System.out.println(Arrays.asList(array));
回答by Micha? Niklas
If I understand your question, you want to "flatten" those arrays to one array. Look at rosettacode.orgfor such example in Java and other languages.
如果我理解您的问题,您想将这些数组“展平”为一个数组。在rosettacode.org 上查看 Java 和其他语言中的此类示例。
回答by bragboy
use the following syntax
使用以下语法
int[][] storeAllArray = {array1, array2, array3, array4};
回答by Seth M.
To access a single element of the selected array you need to do something like:
要访问所选数组的单个元素,您需要执行以下操作:
storeAllArray[i][j]
回答by Robert
int array1[] = { 1, 2, 3, 4, 5, 100, 200, 400 };
int array2[] = { 2, 6, 5, 7, 2, 5, 10 };
int array3[] = { 11, 65, 4, 3, 2, 9, 7 };
int array4[] = { 111, 33, 22, 55, 77 };
int[][] storeAllArray = new int[][] { array1, array2, array3, array2 };
for (int j : storeAllArray[0]) {
System.out.print(j + ", ");
}
回答by polygenelubricants
Create array-of-arrays:
创建数组数组:
int[] array1 = {1,2,3,4,5,100,200,400};
int[] array2 = {2,6,5,7,2,5,10};
int[] array3 = {11,65,4,3,2,9,7};
int[] array4 = {111,33,22,55,77};
int[][] allArrays = {
array1, array2, array3, array4
};
System.out.println(java.util.Arrays.toString(allArrays[0]));
// prints "[1, 2, 3, 4, 5, 100, 200, 400]"
回答by Shubhabrata Ghosh
There is no need to do so. You can use a two dimensional array for the job. Make sure the row length should be equal to the array with max length.
没有必要这样做。您可以为作业使用二维数组。确保行长度应等于具有最大长度的数组。
int a[]={1,2,3,4,5,6};
int b[]={4,8,3,6,4,5};
int c[][]=new int[2][6];//here 2 refers to the no. of arrays and 6 refers to number of elements in each array