miguelacio
miguelacio

Reputation: 150

Java - Split an array of arrays into multiple arrays

Hello fellow programmers, I am having trouble with this logic, what I want to do is split an array of arrays into multiple arrays in an Android Studio application (java).

I have an array like this:

int[][] goal = { {1, 2, 3}, {4, 5, 6}, {0, 7, 8} };

And I want it to look like this:

int[] zero = {1,2,3};
int[] one = {4,5,6};
int[] two = {0,7,8};

But I am having trouble cause i though i could set the array like this

int[] zero = goal [1][];
int[] one = goal [2][];
int[] two = goal [3][];

And I cant, so i am getting here to know if any of you guys could me with this.

Thanks.

Upvotes: 1

Views: 762

Answers (4)

You can get any value of individual arrays and goal can be of any dimension.

int[][] goal =  {{1, 2, 3}, {4, 5, 6}, {0, 7, 8} };

        for(int i=0; i<goal.length; i++){
            int [] actualArray = goal[i];
            for(int j =0; j< actualArray.length; j++){
                // you can get each value of individual array with the following code: actualArray[j];
            }
        }

Upvotes: 1

SindreKjr
SindreKjr

Reputation: 259

Your thinking is correct, but your implementation is wrong. Try:

int[] zero = goal[0];

Upvotes: 2

Rishav Agarwal
Rishav Agarwal

Reputation: 96

You should write-

int[] zero = goal[0];
int[] one = goal[1];
int[] two = goal[2];  

Hope this helps!

Upvotes: 3

lealceldeiro
lealceldeiro

Reputation: 14958

Following your logic...

Change this:

int[] zero = goal [1][];
int[] one = goal [2][];
int[] two = goal [3][];

to this:

int[] zero = goal [0];
int[] one = goal [1];
int[] two = goal [2];

Upvotes: 5

Related Questions