3

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[])?

2 Answers 2

6

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().

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, that is odd to me. I wouldn't think an accessor method would actually be a mutator. I can't believe it was that simple. I see how it makes sense now, in essence it's getting the Object array through the nameOfArray.get() and the brackets with an int contained is the same as using the Object[int]= command. Thanks!
@Geoff - get is merely returning the array, a read-only operation. What you do with the array after that is your business. :) The above is equivalent to Object[] arr = nameOfArray.get(row); arr[col] = ...;
0

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

2 Comments

That's setting an entire row. I think the OP wants to set a particular row,column.
The first line to append should be nameOfArray.add(<object array>) instead of set. The latter always requires an index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.