views:

100

answers:

4

We are working on a lab assignment for my CS&E class and I think I can ask this question without going into detail of the entire lab requirements, but is it possible for an array to be inside of an array? For example, would this work:

int [] arrayOne = new int[3];

arrayOne[0] = Start of an array

If this is possible how do you go about doing it?

+2  A: 

Well, the way you put it, it won't work, you have to declare arrayOne to be a multidimensional array, just like this:


int arrayOne [][] = new int[3][];
arrayOne [0] = new int[5];

if you declare your array like this:


int [] arrayOne = new int[3];

arrayOne will be able to store only the int type, but when you declare it like i said, means that each element in arrayOne can hold another array of the int type;

marcos
Thank you! I now found the chapter in my Java book and hopefully I can take it from here. But thanks again for the quick response!
Adam
+1  A: 

sure

int[][] array2d = new int[3][];
for (int i = 0; i < array2d.length; ++i)
    array2d[i] = new int[4];
Pointy
Thank you! I now found the chapter in my Java book and hopefully I can take it from here. But thanks again for the quick response!
Adam
A: 

use 2 dimensional array.

int[][] arrayOne = new int[3][];
arrayOne[0] = new int[3];
Joset
A: 

You are looking for arrays of arrays also called multi-dimensional arrays which are described in The Java Tutorial page about arrays.

msw