1

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
  • What do you need mutable Strings for?
    – user207421
    Nov 4, 2010 at 0:12

4 Answers 4

7

StringBuilder is a mutable string representation.

A mutable sequence of characters.

It is the unsynchronized (and more efficient) version of StringBuffer

1
  • 1
    (Note that valid uses of StringBuffer other than for backward compatibility are very, very rare.) Nov 3, 2010 at 12:29
4

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.

1
  • Thanks, this seems like the implementation I want.
    – Brad
    Nov 3, 2010 at 10:20
2

If you want to change (replace) a list element you could add

public String setString(int index,String str) 
    { 
        return m_stringList.set(index, str); 
    } 

And call:

String strToChange = gst.setString(2,"eleventy-one"); 

instead of:

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

Thats because your Vector holds a reference to an immutable String, the reference can be replaced by a another String, the String itself can't be changed.

0

Ther's not a reference copy in Java. You can do a new method to GetString class to set a new String to a determined index.

Example: gst.changeString(index, "NewString");

changeString internally set reference to "NewString".

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.