1

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.

1
  • 10
    int[] zero = goal[0], one = goal[1], two = goal[2]; Commented Jan 23, 2018 at 20:12

4 Answers 4

5

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];
Sign up to request clarification or add additional context in comments.

Comments

3

You should write-

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

Hope this helps!

Comments

2

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

int[] zero = goal[0];

Comments

1

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];
            }
        }

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.