更新其他字符串时更新连接字符串

时间:2015-08-14 15:11:03

标签: java

来源

public class TestingPointers {

    private String a;
    private static String b = "b";
    private String c;
    private static String all = intensiveProcess(); //It would be preferable if the intensiveProcess() method only had to be run once.

    public static void main(String args[]) {
        System.out.println(all);
        b = "z";
        System.out.println(all);
    }

    public String intensiveProcess() {
       String out = "";
       for (int i = 0; i < 25000000000000; i++) {
          //a = random words
          out += a;
       }
       out += b;
       for (int i = 0; i < 25000000000000; i++) {
          //c = random words
          out += c;
       }
       return out;
    }
}

输出

RandomWords-b-RandomWords
RandomWords-b-RandomWords

所需输出

RandomWords-b-RandomWords
RandomWords-z-RandomWords

问题

有没有办法更新all,而不必在每次更新a时重新分配cb?即all应指向b而不是包含其值本身。

3 个答案:

答案 0 :(得分:0)

<强>修

因为字符串是不可变的,所以不会复制它们的引用。要解决这个问题,你可以使用StringBuffer,但是因为你想更新一个字符串并让它反映在另一个字符串上,你需要对StringBuffer进行一些封装来完成它。

class StringChain  {

    private StringBuffer[] buffers;

    public StringChain(StringBuffer[] buffers) {
        this.buffers = buffers;
    }

    public String toString() {
        String finalStr = "";
        for (StringBuffer buf : buffers)
            finalStr += buf;
        return finalStr;
    }
}

然后你可以创建该对象并添加StringBuffer。当你想要获取整个字符串时调用toString方法

public static void main(String []args){
    StringBuffer a = new StringBuffer("a"); //Create StringBuffer a
    StringBuffer b = new StringBuffer("b"); //Create StringBuffer b
    StringChain chain = new StringChain(new StringBuffer[] { a, b }); //Pass both buffers to the String Chain
    System.out.println(chain);
    setStringBufferValue(b, "c");
    System.out.println(chain);
 }

 private static void setStringBufferValue(StringBuffer buf, String value) {
     buf.replace(0, buf.length(), value);
 }

这将打印:

  

AB

     

AC

这是StringBuilder的替代方案,通常建议使用StringBuffer。所有需要做的就是替换StringBuffer而不是StringBuilder。

class StringChain  {

    private StringBuilder[] builders;

    public StringChain(StringBuilder[] builders) {
        this.builders = builders;
    }

    public String toString() {
        String finalStr = "";
        for (StringBuilder b : builders)
            finalStr += b;
        return finalStr;
    }
}

....

public static void main(String []args){
    StringBuilder a = new StringBuilder("a"); //Create StringBuilder a
    StringBuilder b = new StringBuilder("b"); //Create StringBuilder b
    StringChain chain = new StringChain(new StringBuilder[] { a, b }); //Pass both builders to the String Chain
    System.out.println(chain);
    setStringBuilderValue(b, "c");
    System.out.println(chain);
 }

 private static void setStringBuilderValue(StringBuilder b, String value) {
     b.replace(0, b.length(), value);
 }

答案 1 :(得分:-2)

每次都手动使all字符串无效:

public class TestingPointers {

    private static String a = "a";
    private static String b = "b";
    private static String all = null;

    public static void setA(String newA) {
        a = newA;
        all = null;
    }

    public static void setB(String newB) {
        b = newB;
        all = null;
    }

    public static String getAll() {
        if(all == null) {
            all = a+ b;
        }
        return all;
    }
}

答案 2 :(得分:-2)

使用方法返回串联?

public static getAll() {
    return a + b:
}