user288609
user288609

Reputation: 13015

2D dynamic array using ArrayList in Java

I need to implement a 2D dynamic array. The number of rows is fixed, say n. But the number of columns for each row is not fixed and equivalent. For instance, the first row has 3 elements and the second row has 5 elements. How to do this in Java using Arraylist. Thanks.

Upvotes: 4

Views: 12396

Answers (9)

CodeRedd
CodeRedd

Reputation: 303

As you say, you can make an array of arraylists and use the ArrayList(int initial capacity) constructor to set the capacity of each column:

ArrayList<YourObject>[] rows=new ArrayList<YourObjects>[n];
for(i=0;i<n;i++){
rows[i]=ArrayList<YourObjects>(initialsize);
}

Upvotes: 0

x4u
x4u

Reputation: 14077

You can either use a array for the rows since this dimenstion is fixed:

@SuppressWarnings("unchecked")
ArrayList<T>[] arr = new ArrayList[ fixedsize];

or use nested ArrayLists:

List<List<T>> list = new ArrayList<List<T>>( fixedsize );

Upvotes: 2

Doug
Doug

Reputation: 390

I would create an array of ArrayList (ArrayList[3] rows = new ArrayList[3] if the rows were 3) Then for each row create column classes and insert them into an ArrayList. and then place the ArrayList into the Array. the row array's index can be used to keep track of the row number. Remember arrays start there indexes at 0 so the row number would be rows[index+1]

Upvotes: 0

trutheality
trutheality

Reputation: 23455

List<ArrayList<SomeObject>> twoDList = new ArrayList<List<SomeObject>>(n);
for( int i=0; i<n; i++ )
    twoDList.add( new ArrayList<SomeObject>() );

Use as:

twoDList.get(rownumber).add(newElementInColumn);

Upvotes: 0

MarioP
MarioP

Reputation: 3832

ArrayList<ArrayList<SomeObject>> twodlist = new ArrayList<ArrayList<SomeObject>>();
ArrayList<SomeObject> row = new ArrayList<SomeObject>();
row.add(new SomeObject(/* whatever */));
// etc
twodlist.add(row);
row = new ArrayList<SomeObject>();
// etc

Upvotes: 2

erickzetta
erickzetta

Reputation: 691

if the number of rows is fixed, try something like this:

ArrayList<MyObject>[] = new ArrayList<MyObject>[fixedRows]

Upvotes: 0

Matt Razza
Matt Razza

Reputation: 3654

You could create an array of ArrayList elements because your row count is fixed.

ArrayList[] dynamicArray = new ArrayList[n]();

Note: You'll need to allocate an ArrayList object in each entry in the array. So...

for (int loop = 0; loop < n; loop++)
dynamicArray[loop] = new ArrayList();

OR if you'd like both rows and columns to be dynamic you could create an ArrayList of ArrayLists....

ArrayList<ArrayList<T>> dynamicArray = new ArrayList<ArrayList<T>>();

Once again, you'll need to create an array list in each new entry to dynamicArray.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

Try:

ArrayList<ArrayList<DataType>> array = new ArrayList<ArrayList<DataType>>();
for (int i = 0; i < n; ++i) {
    array.add(new ArrayList<DataType>());
}

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240860

How about List<List<Foo>> ?

For Example:

List<List<Foo>> list = new ArrayList<List<Foo>>();

List<Foo> row1 = new ArrayList<Foo>();
row1.add(new Foo());
row1.add(new Foo());
row1.add(new Foo());
list.add(row1);

List<Foo> row2 = new ArrayList<Foo>();
row2.add(new Foo());
row2.add(new Foo());

list.add(row2);

Upvotes: 3

Related Questions