获取一个字符串作为java中的引用

发布于 2024-09-30 13:55:34 字数 1377 浏览 2 评论 0原文

所以我希望能够在 Java 中拥有一个可变字符串的集合。

我有这个测试类来查看不可变字符串的功能:

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);
    }
}

以下是输出:

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

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

我可以做什么来将这些字符串存储为可变的?我正在考虑有一个 StringObject 类,它只包含对 String 的引用。这是最好的选择吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱花坊 2024-10-07 13:55:34

StringBuilder是可变的字符串表示形式。

可变的字符序列。

它是 StringBuffer 的非同步(且更高效)版本

StringBuilder is a mutable string representation.

A mutable sequence of characters.

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

翻身的咸鱼 2024-10-07 13:55:34

我可以建议您使用 StringBuffer

从API:

线程安全、可变的字符序列。 字符串缓冲区类似于字符串,但可以修改。在任何时间点,它都包含某些特定的字符序列,但可以通过某些方法调用更改该序列的长度和内容。

请注意,如果您不打算使用它,最好使用 StringBuilder 因为它是不同步的并且速度更快。

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.

八巷 2024-10-07 13:55:34

如果你想改变(替换)一个列表元素,你可以添加

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

And call:

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

而不是:

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

那是因为你的 Vector 持有对不可变字符串的引用,该引用可以被另一个字符串替换,字符串本身不能改变。

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.

红墙和绿瓦 2024-10-07 13:55:34

没有 Java 的参考副本。您可以对 GetString 类执行一个新方法,将新的 String 设置为确定的索引。

例子:
gst.changeString(index, "NewString");

changeString 在内部设置对“NewString”的引用。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文