Reputation: 13
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?
Upvotes: 1
Views: 5699
Reputation: 43497
You are looking for arrays of arrays also called multi-dimensional arrays which are described in The Java Tutorial page about arrays.
Upvotes: 0
Reputation: 3640
use 2 dimensional array.
int[][] arrayOne = new int[3][];
arrayOne[0] = new int[3];
Upvotes: 0
Reputation: 413737
sure
int[][] array2d = new int[3][];
for (int i = 0; i < array2d.length; ++i)
array2d[i] = new int[4];
Upvotes: 2
Reputation: 11328
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;
Upvotes: 2