获取一个字符串作为java中的引用
所以我希望能够在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
StringBuilder
是可变的字符串表示形式。它是
StringBuffer
的非同步(且更高效)版本StringBuilder
is a mutable string representation.It is the unsynchronized (and more efficient) version of
StringBuffer
我可以建议您使用
StringBuffer
。
从API:
请注意,如果您不打算使用它,最好使用
StringBuilder
因为它是不同步的并且速度更快。May I recommend you to use a
StringBuffer
.From the API:
Note that if you don't plan to use it in you're better off with a
StringBuilder
as it is unsynchronized and faster.如果你想改变(替换)一个列表元素,你可以添加
And call:
而不是:
那是因为你的 Vector 持有对不可变字符串的引用,该引用可以被另一个字符串替换,字符串本身不能改变。
If you want to change (replace) a list element you could add
And call:
instead of:
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.
没有 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".