Java 为什么一个StringBuffer对象中的更改会影响另一个?这些有共享内存吗
我是java新手。我正在努力做作业。在本文中,我首先创建了StringBuffer对象strB1和strB2。从用户处获取输入后。我创建了一个新的StringBuffer对象,并将strB1的内容复制到该对象。我正在对包含strB1内容的新stringbuffer对象strobj进行所有修改。但新物体的变化反映在原始物体上。请帮忙。我无法理解为什么原始对象正在被改变 在代码末尾,您将看到打印两个对象产生相同的结果,而我只对一个对象进行了更改Java 为什么一个StringBuffer对象中的更改会影响另一个?这些有共享内存吗,java,stringbuffer,Java,Stringbuffer,我是java新手。我正在努力做作业。在本文中,我首先创建了StringBuffer对象strB1和strB2。从用户处获取输入后。我创建了一个新的StringBuffer对象,并将strB1的内容复制到该对象。我正在对包含strB1内容的新stringbuffer对象strobj进行所有修改。但新物体的变化反映在原始物体上。请帮忙。我无法理解为什么原始对象正在被改变 在代码末尾,您将看到打印两个对象产生相同的结果,而我只对一个对象进行了更改 import java.util.Scanner;
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());
}
}
import java.util.Scanner;
公共级Homwork1_Q2{
StringBuffer strobj2;
Homwork1_Q2()
{
}
//第2部分和第3部分或hw中使用的复制构造函数
Homwork1_Q2(StringBuffer strobj_2)
{
this.strobj2=strobj_2;
}
公共静态void main(字符串参数[])
{
//创建两个StringBuffer对象
StringBuffer strB1=新的StringBuffer();
StringBuffer strB2=新的StringBuffer();
//1.创建扫描仪的obj并在STRB1中输入
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入长字符串”);
strB1.append(scan.nextLine());
//输入一个较短的字符串
System.out.println(“输入短字符串”);
strB2.append(scan.nextLine());
//如果第二个stringBuffer较长,则第一个到
//异常,然后再次获取输入
尝试
{
if(strB1.length()
要复制,您不想使用赋值运算符。相反,您希望像这样使用复制构造函数:
strobj = new StringBuffer(strB1);
您还可以在Homwrk1_Q2的复制构造函数中使用赋值运算符
Homwork1_Q2(StringBuffer strobj_2)
您不仅要复制StringBuffer的内容,而且还要将StringBuffer对象的引用复制到其他StringBuffer。根据您的代码,这意味着变量strobj和strB1都指向同一个内存位置。改变其中一个也会改变另一个。我希望这有帮助。一定要告诉我你的思维过程 你做一个
strobj=strB1代码>在您的代码中。将strobj
和strB1
设置为相同的引用。那是故意的吗?不,那不是故意的。我想把strB1的内容复制到strobj。谢谢你解决了这个问题。你能告诉我如何将一个StrB1的内容复制到Strobj吗?请不要使用文本输出的屏幕截图。顺便说一下,StringBuffer
已经过时了。从十三年前开始,您应该使用StringBuilder
。当我们使用strobj=newstringbuffer(strB1)时。它是否会分配一个新的StringBuffer,strobj现在将引用新的StringBuffer,而以前的StringBuffer将由java的自动垃圾收集器收集?