reesjones
reesjones

Reputation: 704

How do I get the index of a 2d arraylist to be used in the ArrayList.set() method?

I have a 2D ArrayList and I want to replace one of the elements with something else by using the set method.

According to the Java API:

set(int index, E Element)

is the constructor. How do I input the index, given that the ArrayList is 2D?

Upvotes: 0

Views: 2435

Answers (2)

sha1
sha1

Reputation: 143

I came across this while searching:

array.get(i1).put(i2, value);

I think, it can be modified like this:

array.get(i1).set(i2, value);

I hope it works.

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198023

If you have a two-dimensional ArrayList, then what you need to do is

outerList.get(x).set(y, newValueAtXY);

...which gets the row at index x and sets element y in that row to newValueAtXY.

Upvotes: 3

Related Questions