Geoff
Geoff

Reputation: 256

Set value in ArrayList<Object[]>

It's been a while since my data structures class and I'm having a hard time figuring this one out. I know that if I want to get the value from an ArrayList of Object[] I can use

nameOfArrayList.get(row)[col];

but I can't seem to figure out, or find on the web, how to set a value.

Would I have to create a new object and set it to nameOfArrayList.get(row), alter the value and then use

nameOfArrayList.set(row,Object[])?

Upvotes: 3

Views: 34237

Answers (2)

S.P.
S.P.

Reputation: 3054

Here is a simple basic run.

If you want to add the Object at the end you can just use something like

nameOfArray.add(<object array>)

Else if you want it at a specified index something like above

nameOfArray.set(index, <object array>)

Reference

<object array>.get(row)[col] = change value

Upvotes: 0

Adam Zalcman
Adam Zalcman

Reputation: 27233

Try this:

nameOfArray.get(row)[col] = ...;

This sets a new value at index col of the array at index row in the nameOfArray ArrayList.

If you want to put a new array into nameOfArray at position row, try this:

nameOfArray.set(row, new Object[]);

This creates a new array of objects and puts it under index row in nameOfArray. See ArrayList.set().

Upvotes: 6

Related Questions