0

In Java, I have data that needs to be accessed as a one dimension array some of the time and as a two dimension array at other times. In other words, I would like to be able to map the same data as both a normal array and as a two dimension array.

Since a two dimension array in Java is actually an array of arrays, I would like to elements in the array of arrays to references of elements from the one dimension array.

Something like:

int  height = 5;
int  width  = 10;
int  length = height * width;

int[]   oneD = new int[length];
int[][] twoD = new int[height][];

int i, j;
for (i = j = 0 ; i < height ; ++i, j += width)
{
    // Following Statement fails to compile.
    twoD[i] = oneD[j];  // assign reference, not value
}

The problem is that the statement "twoD[i] = oneD[j]" fails because of types do not match. The statement "twoD[i][0] = oneD[j]" failes because twoD[i] is null. "twoD[i][] = oneD[j]" also fails to compile.

In C++, this is an easy problem where you would create the array of arrays as an array of pointers and then set each element of the array of pointers to the address of an element in the one dimension array.

My questions are:

  1. Is there a way to map the array of arrays in the two dimension array so that each of the arrays is a section of the one dimension array? In other words, can you access the reference of an element in an array and assign it to an array of arrays element?

  2. Is this possible to do this in Java without writing a class that encapsulates the data and provides one and two dimensional indexing to the data?

  3. Or, is there a better way of accomplishing the equivalent thing using container classes instead of arrays?

Factors:

  • Using Java 8

  • Am not concerned if the two dimension array can provide the correct length for each array (of columns).

1 Answer 1

2
  1. No. In Java there is no way to take a 'reference' to the middle of an array. The only access is through through normal indexing.

  2. You don't need to write a class: container classes are fine for this.

  3. Yes. If you create, say an ArrayList then you can use List.subList to return a reference to a section of the original list. The only trick here is that the list needs to actually have elements in it before you take the sublists.

So something like:

List<Integer> list = IntStream.range(0, 50).boxed().collect(toList());
List<List<Integer>> sublists = IntStream.range(0, 5)
    .mapToObj(i -> list.subList(i * 10, (i + 1) * 10)).collect(toList());

System.out.println(subLists.get(2));

Prints out [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

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

1 Comment

I was afraid that what I wanted to do was not possible in Java. You solution using List<E> will do what I need . My Java reference book states that changes in the backing List may not be reflected in the sublists. My book is old and was written for Java 5. The Java 8 documentation states that changes in either the backing List or any of the sublists are reflected in the other. Nice improvement from the older version.

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.