1

I have 2d array called myarr[3][3] and I want to to get myarr[0][0], myarr[0][1], and myarr[0][2] in another 1d array called arr1[3]. The same for myarr[1][0], myarr[1][1], and myarr[1][2] in another 1d array called arr2[3], and so on.

How can I do something like that in Java?

I have tried to use for loop but I stopped and couldn't know what to write inside it.

for(int i=0 ; i<3 ; i++){
    for(int j=0 ; j<3 ; j++){
        //what can i write here
    }
}

1 Answer 1

1

Instead of using an explicit for loop, you could use System.arraycopy(). These are the method signature and description:

arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

Then, to copy the contents from myarr[0] to arr1 you could do:

System.arraycopy(myarr[0], 0, arr1, 0, arr1.length);
Sign up to request clarification or add additional context in comments.

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.