为什么一个StringBuffer对象的更改会影响另一个?这些是否有共享内存

时间:2017-09-07 15:31:40

标签: java stringbuffer

我是java的新手。我正在尝试做作业。在这里我首先创建了StringBuffer对象strB1和strB2。从用户那里获取输入后。我创建了一个新的StringBuffer对象,并将strB1的内容复制到该对象。我正在对包含strB1内容的新stringbuffer对象strobj进行所有修改。但新物体的变化反映在原始物体中。请帮忙。我无法理解为什么原始物体会被改变。

您将在代码打印结束时看到两个对象都产生相同的结果,而我只在一个对象中进行更改。

import java.util.Scanner;

public class Homwork1_Q2 {

    StringBuffer strobj2;

    Homwork1_Q2()
    {

    }
    // Copy constructor used in part 2 and 3 or hw
    Homwork1_Q2(StringBuffer strobj_2)
    {
        this.strobj2 = strobj_2;
    }

    public static void main(String args[])
    {
        // create two StringBuffer objects
        StringBuffer strB1 = new StringBuffer();
        StringBuffer strB2 = new StringBuffer();

        //1. create a obj of Scanner and take input in STRB1
        Scanner scan = new Scanner(System.in);
        System.out.println("Input the  Long String");
        strB1.append(scan.nextLine());

        //Input a shorter String
        System.out.println("Input the  Short String");
        strB2.append(scan.nextLine());

        //If 2nd stringBuffer is longer the first through an
        //exception and again take the input
        try
        {
        if(strB1.length() < strB2.length())
        {
            throw new Exception();

        }
        }catch(Exception e)
        {
            System.out.println("2nd String should be shorter.. Input again");
            strB2.append(scan.nextLine());
        }


        // 2. Create a StringBuffer object from the long String
        StringBuffer strobj = new StringBuffer();
        strobj = strobj.append(strB1.toString());

        //3. Using the StringBuffer with the appropriate specific constructor.
        Homwork1_Q2 object = new Homwork1_Q2(strB1);

         //4. Position of the small string in the long string
        //If more then one position is present then it will calculate that too 
        int position;
        int check = 0;

        while((strobj.indexOf(strB2.toString()))!=(strobj.lastIndexOf(strB2.toString())))
        {
            position = strobj.indexOf(strB2.toString());
            System.out.println("Small String is present at position "+ (position+check));
            strobj.delete(position, position+strB2.length());
            check = check+strB2.length();
        }

        position = strobj.indexOf(strB2.toString());
        System.out.println("Small String is present at position "+(position+check));
        strobj = strB1;

        //5. Delete the small string
        //If more then one time small string is present in large string then it will delete them too 
        while((strobj.indexOf(strB2.toString()))!=(strobj.lastIndexOf(strB2.toString())))
        {
            position = strobj.indexOf(strB2.toString());
            strobj.delete(position, position+strB2.length());
            check = check+strB2.length();
        }

        position = strobj.indexOf(strB2.toString());
        strobj.delete(position, position+strB2.length());
        check = check+strB2.length();
        System.out.println(strobj.toString());

        System.out.println(strB1.toString());


    }
}

enter image description here

2 个答案:

答案 0 :(得分:1)

要复制,您不想使用赋值运算符。相反,你想使用像这样的复制构造函数:

strobj = new StringBuffer(strB1);

您还可以在Homwrk1_Q2

的复制构造函数中使用赋值运算符
Homwork1_Q2(StringBuffer strobj_2)

答案 1 :(得分:0)

您不仅要复制StringBuffer的内容,而且还要将StringBuffer对象的引用复制到其他StringBuffer中。根据您的代码,这意味着两个变量 strobj,strB1 都指向相同的内存位置。更改其中一个也会更改另一个。希望对您有所帮助。告诉我您的思考过程!