How to store arrays in one array

How to store arrays in one array? for example I have four different arrays, I want to store them in one array int storeAllArray []and when I call, for example. storeAllArray [1], will I get this output [11,65,4,3,2,9,7]instead of individual items?

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 to store the entire array in an array

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

Editorial: How can I get the result as follows?

   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];
+3
source share
7 answers
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

[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

    System.out.println(Arrays.asList(array));
+6
source

If I understand your question, you want to "smooth out" these arrays into one array. See rosettacode.org for such an example in Java and other languages.

+1
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 + ", ");
    }
+1

int[][] storeAllArray = {array1, array2, array3, array4};
0
source

To access one element of the selected array, you need to do something like:

storeAllArray[i][j]
0
source

Create an 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]"
0
source

There is no need to do this. You can use a two-dimensional array for the job. Make sure the string length should be equal to the array with the maximum 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
0
source

Source: https://habr.com/ru/post/1739592/


All Articles