3

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

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];
Jessy
  • 15,321
  • 31
  • 83
  • 100
  • possible duplicate of [How to flatten 2D array to 1D array?](http://stackoverflow.com/questions/2569279/how-to-flatten-2d-array-to-1d-array) – givanse Dec 19 '13 at 16:23
  • @givanse Have to disagree here. The question you linked is about turning a 2D array into a 1D array, when this question is really asking about making a 2D array. – Dennis Meng Dec 19 '13 at 16:48

7 Answers7

8
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));
Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • @Jessy Have you by any chance tried to use `Arrays.asList(array)` with Java 5 or above? That won't work - use `Arrays.toString(array)` instead. – Péter Török Apr 02 '10 at 13:41
1

If I understand your question, you want to "flatten" those arrays to one array. Look at rosettacode.org for such example in Java and other languages.

Michał Niklas
  • 53,067
  • 18
  • 70
  • 114
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 + ", ");
    }
Robert
  • 8,406
  • 9
  • 38
  • 57
0

use the following syntax

int[][] storeAllArray = {array1, array2, array3, array4};
bragboy
  • 34,892
  • 30
  • 114
  • 171
0

To access a single element of the selected array you need to do something like:

storeAllArray[i][j]
Seth M.
  • 598
  • 2
  • 4
  • 14
0

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]"
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
-1

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