开发者

Array inside of Arrays; Java (Help with Lab Assignment)

We are working on a lab assignment for my CS&E class and I think I can ask this question without go开发者_运维知识库ing 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?


sure

int[][] array2d = new int[3][];
for (int i = 0; i < array2d.length; ++i)
    array2d[i] = new int[4];


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;


use 2 dimensional array.

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜