0

Okay, I have had a question, my code has seemed to work, but I have not tested it particuarly well.

I'm trying to set an element in an ArrayList.

ArrayList<StringBuilder> g=new ArrayList<StringBuilder>();
//set the array contents
g.get(2).append("Something");

I know that doing something like

StringBuilder q=g.get(2);
q.append("something else?");
g.set(2,q);

works, and is probably the right way to do it, but it seems like such long way of doing it.

Am I doing this right, if I'm not, then is the second way I've mentioned the only way?

1
  • You don't need to set the same StringBuilder back to the list as it won't do anything. Commented Dec 15, 2012 at 21:14

3 Answers 3

2

If you have a list of StringBuilder, which are modified in place, then the first method is fine. If you have a list of some immutable type (like String), then since you cannot change the object that you get out, you have to use a variant of the second method, because the result is not the same object.

e.g.

List<StringBuilder> widgets = new ArrayList<StringBuilder>();
// ...
StringBuilder widget = widgets.get(0);
widget.append(" version 2");

vs

List<String> widgets = new ArrayList<String>();
// ...
String widget = widgets.get(0);
widgets.set(2, widget + " version 2");
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, though I wasn't actually asking about StringBuilders in general, more the overall practice of modifying items in lists of mutable items.
@jaked122 You should tell this in your question then.
Why, the stringbuilder class has no special treatment in java, so thus any advice given on this would work on most, if not all classes.
If you mutate an item in the list, then you don't need to store it again. If you take an item and compute some new object (e.g. because the original was immutable), then you need to call set. StringBuilder and String were simply examples of mutable and immutable objects (inspired by your question).
1

When calling List#get() method you are retrieving a reference to your StringBuilder object. StringBuilder is a mutable object. So if you want to modify your StringBuilder's contents at index 2 then you don't need to set a reference again after you modify it. Hence the following is enough:

g.get(2).append("Something");

Comments

0

g.get(2).append("Something"); works if you initialized the list values with StringBuilder instances. Otherwise you will get a NullPointerException.

Note that in your code you define an ArrayList of type StringBuilder but then you try to assign an ArrayList of type Integer to it, which does not work

2 Comments

Yeah, I was thinking of a mutable type that would work, Integer didn't work, so I partially changed the code to StringBuilder.
Both methods will only work if the list is initialized; they behave the same with the difference that the second one does and unnecessary set.

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.