2

I need some help getting started. I have one array storing several arrays (in this case, lets say 3, this may be a lot more or less):

mainarray {arr, arr, arr}

All the arrays within the mainarray have the same amount of values, but this may also change to fewer or less Lets say I have following arrays:

mainarray {
  arr{"1","2","3"};
  arr{"One","Two","Three"};
  arr{"Red","Blue","Yellow"};
}

Everytime I try to make this Object[][] the sorting comes out like this:

Object {{"1","2","3"}, {"One","Two","Three"}, {"Red","Blue","Yellow"}};

Here comes my problem: Is there a way of making this arrays into an Object[][], but with this sorting?

Object {{"1","One","Red"}, {"2","Two","Blue"}, {"3","Three","Yellow"}};

Thanks in advance!

1
  • You can translate the arrangement of the arrays using a couple of loops. What have you tried? Commented Nov 23, 2013 at 18:05

2 Answers 2

2

Here you go

    Object arr1[] = { "1", "2", "3" };
    Object arr2[] = { "One", "Two", "Three" };
    Object arr3[] = { "Red", "Blue", "Yellow" };
    //make an array of arrays
    Object allArr[] = { arr1, arr2, arr3 };
    //the new array to store values
    Object arr4[][] = new Object[3][3];

    for (int i = 0; i < 3; i++) {
        for (int j = 0, k = 0; j < 3 && k < 3; j++, k++) {
            //take the first array from array of arrays
            Object tmp[] = (Object[]) allArr[k];
            //take the ith element of each individual array and store in the new array in correct position 
            arr4[i][j] = tmp[i];

        }
    }
     //print the new array
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            System.out.print(arr4[i][j] + " ");
        }
        System.out.println();
    }

This works for your example. You can generalise it further.

output

1 One Red 
2 Two Blue 
3 Three Yellow 
Sign up to request clarification or add additional context in comments.

2 Comments

change < 3 to < allArr.length and change new Object[3][3] to new Object [arr1.length][allArr.length]. Make it work for all sizes of arrays.
@mikeyaworski let OP do that :-) Thanks for suggestion
2

Code

This code works for arrays of all sizes, assuming that they are of equal sizes though (as you said).
This is just a modified version of n1234's code.

Object arr1[] = { "1", "2", "3"};
Object arr2[] = { "One", "Two", "Three"};
Object arr3[] = { "Red", "Blue", "Yellow"};

Object allArr[] = { arr1, arr2, arr3 }; // combination of the arrays

Object arr4[][] = new Object[arr1.length][allArr.length]; // array to be rearranged

// define the new, rearranged array
for (int i = 0; i < arr4.length; i++) {
    for (int j = 0, k = 0; j < arr4[i].length && k < arr4[i].length; j++, k++) 
    {
        Object tmp[] = (Object[]) allArr[k];
        arr4[i][j] = tmp[i];
    }
}       

// print the array
for (Object r[] : arr4) {
    for (Object c : r)
        System.out.print(c + " ");
    System.out.println("");
}

Input/Output

Input:

Object arr1[] = { "1", "2", "3", "4"};
Object arr2[] = { "One", "Two", "Three", "Four"};
Object arr3[] = { "Red", "Blue", "Yellow", "Green"};

Output:

1 One Red
2 Two Blue
3 Three Yellow


Input:

Object arr1[] = { "1", "2", "3"};
Object arr2[] = { "One", "Two", "Three"};
Object arr3[] = { "Red", "Blue", "Yellow"};

Output:

1 One Red
2 Two Blue
3 Three Yellow
4 Four Green

3 Comments

you could have left some work for OP :-). It is basically same code as mine :-)
@n1234 I also showed him how to use the other version of the for loop, which is much cleaner for printing. And I think I should give him all of what he asked for, considering he said the array sizes could be different. But yes, it's just a modified version of yours. I should give you credit.
Thanks for answers! Both were right, but I gave credit to @n1234, because he was the first one out :)

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.