0

Consider a nested array in java. Does the top level of the array contain references to the inner arrays, or does the memory actually contain the inner arrays themselves?

If you need an illustration, assume I have access to a reverse method, which will reverse an array in place by doing multiple swaps. If I call reverse on the top-level of an M*N nested array, what will that method simply swap references around (an O(m) operation), or will it be swapping entire rows around (an O(m*n) operation)?

2 Answers 2

3

In a word references. Arrays themselves are likely to be contiguous blocks, but it's unlikely that the Objects the elements refer to are.

This article sums it up nicely http://java.dzone.com/articles/what-does-java-array-look

Sign up to request clarification or add additional context in comments.

Comments

1

I believe you can find the answer in most introductory Java book (although it may be not that obvious).

In Java, nested/multi-dimension array is not a continuous block. It is simply an array of "reference to array".

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.