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!

Mr.Turtle
  • 2,950
  • 6
  • 28
  • 46

2 Answers2

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 
Nishant
  • 1,142
  • 1
  • 9
  • 27
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

Community
  • 1
  • 1
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • you could have left some work for OP :-). It is basically same code as mine :-) – Nishant Nov 23 '13 at 19:00
  • @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. – Michael Yaworski Nov 23 '13 at 19:03
  • Thanks for answers! Both were right, but I gave credit to @n1234, because he was the first one out :) – Mr.Turtle Nov 23 '13 at 20:26