简体   繁体   中英

get a string as a reference in java

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?

StringBuilder is a mutable string representation.

A mutable sequence of characters.

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

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.

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.

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM