Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9010445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:27:41+00:00 2026-06-16T02:27:41+00:00

Okay, I have had a question, my code has seemed to work, but I

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:27:43+00:00Added an answer on June 16, 2026 at 2:27 am

    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");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I have updated my code a little, but I am still not exactly
Okay, this is like the 5th time I have had to ask this question,
Okay, so I have torn what I had down and am rebuilding it, here's
okay i have been trying to understand this for hours i am learning VB
Okay I have updated my code quite a bit. I am getting a new
Okay i have two models: posts and comments. as you can think comments has
Okay, little oddity I discovered with my C++ compiler. I had a not-overly complex
I had trouble coming up with a title for this question because I'm not
(I edited the whole question to be more clear) Hello, I have never had
Okay, so I have a question. I use print <<< END followed by an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.