开发者

get a string as a reference in java

开发者 https://www.devze.com 2023-01-23 01:39 出处:网络
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:

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 ===");
        Sys开发者_运维百科tem.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".

0

精彩评论

暂无评论...
验证码 换一张
取 消