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 3316866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:31:11+00:00 2026-05-17T22:31:11+00:00

So I want to be able to have a collection of mutable Strings in

  • 0

So I want to be able to have a collection of mutable Strings in Java.

I have this test class to see the functionality of immutable Strings:

public class GetStringTest
{
    private Vector<String> m_stringList;

    public GetStringTest()
    {
        m_stringList = new Vector<String>();

        m_stringList.add("zero");
        m_stringList.add("one");
        m_stringList.add("two");
        m_stringList.add("three");
        m_stringList.add("four");
        m_stringList.add("five");
        m_stringList.add("six");
    }

    public String getString(int index)
    {
        return m_stringList.get(index);
    }

    public String toString()
    {
        String str = "";

        for (String item : m_stringList)
        {
            str += item + "\n";
        }

        return str;
    }

    public static void main(String[] args)
    {
        GetStringTest gst = new GetStringTest();

        System.out.println("=== original content ===");
        System.out.println(gst);

        String strToChange = gst.getString(2); // "two"
        strToChange = "eleventy-one";

        System.out.println("=== with change ===");
        System.out.println(gst);
    }
}

The following is the output:

=== original content ===
zero
one
two
three
four
five
six

=== with change ===
zero
one
two
three
four
five
six

What can I do to store these Strings as mutable? I was thinking of having a StringObject class that would simply contain a reference to a String. Is this the best option?

  • 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-05-17T22:31:12+00:00Added an answer on May 17, 2026 at 10:31 pm

    May I recommend you to use a StringBuffer.

    From the API:

    A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

    Note that if you don’t plan to use it in you’re better off with a StringBuilder as it is unsynchronized and faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of Post objects and I want to be able to
I have a collection (list) of objects. I want to be able to sort
I want to have a collection of functions in the class T, defined below.
I have a collection of ActiveRecord objects. I want to be able to run
I have a collection of strings which I want to select from to insert
Let's say I have a class like this : public class MyClass { public
Let's say I have a collection in backbone and I want to be able
I want to be able to have a text box on a page where
I want to be able to have default text like Enter content here... appear
I want to be able to have a Stored Procedure that can only be

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.