Adam
Adam

Reputation: 13

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 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

Answers (4)

msw
msw

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

setzamora
setzamora

Reputation: 3640

use 2 dimensional array.

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

Upvotes: 0

Pointy
Pointy

Reputation: 413737

sure

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

Upvotes: 2

marcosbeirigo
marcosbeirigo

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

Related Questions