4

I'm having a hard time to figure out on how to merge multidimensional array into one single array.

Here is my code:

        String[][] multiArray = {{"1","2","3"},{"4","5","6"}};
    String[] singleArray = new String[6];

    for(int i=0; i<singleArray.length; i++)
    {
        for(int x=0; x<multiArray.length; x++)
        {
            for(int z=0; z<multiArray[x].length;z++)
            {
                //for(int i=0; i<singleArray.length; i++)
                //{
                singleArray[i] = multiArray[x][z];  
                //}
            }
        }
    }


    for(String temp : singleArray){
        System.out.println(temp);
    }

The RESULT is

6  
6  
6  
6  
6  
6  

Why is that? How can I put all the numbers into one single array? Many thanks!

1
  • check your flow singleArray[i] should be something else like singleArray[k] where k starts from 0 upto no of elements in multiarray Commented Nov 1, 2016 at 7:24

7 Answers 7

3

You need to increment the index of the 1-dimensional array for each entry in the multi-dimensional:

String[][] multiArray = {{"1","2","3"},{"4","5","6"}};
String[] singleArray = new String[6];

for (int x = 0, i = 0; x < multiArray.length; x++) {
    for (int z = 0; z < multiArray[x].length; z++) {
        singleArray[i++] = multiArray[x][z];  
    }
}

Notice that i is initialized in the outer loop, and it's incremented when assigning a value in the 1-dimensional array.

Another equivalent alternative would be incrementing i in the inner loop:

for (int x = 0, i = 0; x < multiArray.length; x++) {
    for (int z = 0; z < multiArray[x].length; z++, i++) {
        singleArray[i] = multiArray[x][z];  
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3
String[][] multiArray = {{"1","2","3"},{"4","5","6"}};
String[] strings = Arrays.stream(multiArray)
        .flatMap(Arrays::stream)
        .toArray(size -> new String[size]);

Comments

1

How about using collections?

    String[][] multiArray = {{"1","2","3"},{"4","5","6"}};

    List<String> result = new ArrayList<>();
    for(String[] firstLevel : multiArray){
        for(String secondLevel : firstLevel){
            result.add(secondLevel);
        }
    }
    return result.toArray();

Comments

1

Your code is not logic. You iterate over all the values of the multidimentional array before incrementing the index of the single array. That's why each item of the single arrays contains the last item of the multidimensional array.

You should avoid itterating over the single array and use a counter to write its values. Try this:

String[][] multiArray = {{"1","2","3"},{"4","5","6"}};
String[] singleArray = new String[6];

int counter=0;
for(int x=0; x<multiArray.length; x++)
{
    for(int z=0; z<multiArray[x].length;z++)
    {
        singleArray[counter] = multiArray[x][z]; 
        counter++: 
    }
}



for(String temp : singleArray){
    System.out.println(temp);
}

Comments

1

Actually in your code, the value of i is not changing, there is no need to have i as parent of all loop you have to increment the value of i, after each updating step.

  int i=0;
  while (i<singleArray.length)
   {
    for(int x=0; x<multiArray.length; x++)
    {
        for(int z=0; z<multiArray[x].length;z++)
        {


            singleArray[i++] = multiArray[x][z];  

        }
    }
}


for(String temp : singleArray){
    System.out.println(temp);
}

Comments

1

In your code, you begin to iterate on the array where you want to store all values. You should not begin to iterate on it since it has not data.
The logic is simple : you should iterate on elements you have, read each one and add it in your destination array.
You begin by a loop for first dimension of the array, then you do an embedded loop for the second dimension of the array and now you can read each data of the two-dimension array.

String[][] multiArray = { { "1", "2", "3" }, { "4", "5", "6" } };
String[] singleArray = new String[multiArray[0].length + multiArray[1].length];

int i = 0;
for (int j = 0; j < multiArray.length; j++) { // first dimension [j]
    for (int k = 0; k < multiArray[j].length; k++) {  // second dimension [k]
      singleArray[i] = multiArray[j][k]; // here you get [j][k] 
      i++; // you go to next position of destination array  
           // when you had an entry
    }   
}

Comments

1

The fault is that the variable i, which denotes the index of singleArray, is updated once in every full iteration of the inner two loops. So, what happens is for every value of i, single[i] keeps getting all the values from 1 to 6 in every iteration of the outermost loop. That's why, all indexes in the 1-D array has the last value i.e., 6. What you need to do is increment i's value using the postfix increment operator every time a value is added to singleArray. Don't do it in a loop.

int i=0; //starting index
for(int x=0; x<multiArray.length; x++)
{
    for(int z=0; z<multiArray[x].length;z++)
    {
        singleArray[i++] = multiArray[x][z];
        /* i's value is changed every time a value is added to
            singleArray */
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.