Topic: 请教一个StringBuffer 问题!!!!!!!!!!!!!

  Print this page

1.请教一个StringBuffer 问题!!!!!!!!!!!!! Copy to clipboard
Posted by: jancyu2008
Posted on: 2008-06-24 21:34


public class Foo{
public static void main(String[] sgf){

StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");

operate(a,b);

System.out.println(a+","+b);//这里要求不能有变动
}

static void operate(StringBuffer x,StringBuffer y){

x.append(y);
y=x;
}
}


输入的结果如何才能变成AB,AB
而不是AB,B

2.Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re: jancyu2008] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-06-25 16:59

String Buffer is thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified.

Having said that, I can see where your confusion is, there is an important theory in Java concerning method invocation:
The local method parameter's object reference will *not* affect the object reference which invokes this method. (Because Java parameter is passed by value, not reference. aka completely two different spaces in memory)
However changing members of the object reference will be affected.

The above explanation sounds intimidating, but it actually not. In your example, object reference x has affected the value by invoking the append method, whereas object reference y does not. So you should avoid using the "=" sign because *it does not take any effect*.

> 输入的结果如何才能变成AB,AB

x.append( y );
y.delete(0, y.length());
y.append(x.toString());


Note this example does not have any practical use, it is only used for demonstrating the method invocation in Java.

Regards,
Jiafan

3.Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re: jancyu2008] Copy to clipboard
Posted by: jancyu2008
Posted on: 2008-06-26 00:12

謝謝版主,我也想了一個解決的辦法,不過和你的相比顯得相形見拙了,呵呵,下面是我的辦法
public class Foo{
public static void main(String sgf[]){

StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");

a.append(b);
b=a;

System.out.println(a+","+b);
}
}

4.Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re: jancyu2008] Copy to clipboard
Posted by: jancyu2008
Posted on: 2008-06-26 00:26

今天又有了狠大的收獲

5.Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re: jancyu2008] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-06-27 16:19

jancyu2008 , there is a convention here people tend to adopt. i.e. put a code tag around the code snippet. Once you done that, the background of your code will look black, as to make others easy to read. Smile

Note there might be an issue from time to time if others edit your content. Hope it can be addressed soon.

Thanks,
Jiafan

6.Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re: jancyu2008] Copy to clipboard
Posted by: jancyu2008
Posted on: 2008-06-28 00:09

Ah ,I don't know what can I do it on the frist
Hehe ,thank you

7.Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re: HenryShanley] Copy to clipboard
Posted by: Joffeec
Posted on: 2008-06-29 11:25

HenryShanley wrote:

> 输入的结果如何才能变成AB,AB

x.append( y );
y.delete(0, y.length());
y.append(x.toString());



版主可以用一个y.delete(0,y.length());先来删掉y里面的内容再用append()的方法把x的内容加进去。。那么我用一个replace()也可以达到同样的效果。

x.append( y );
y.replace(0,1,x.toString());

8.Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re: Joffeec] Copy to clipboard
Posted by: jancyu2008
Posted on: 2008-06-29 23:45

Joffeec wrote:
版主可以用一个y.delete(0,y.length());先来删掉y里面的内容再用append()的方法把x的内容加进去。。那么我用一个replace()也可以达到同样的效果。

x.append( y );
y.replace(0,1,x.toString());


Yes , Hehe ,thanks

9.Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re: jancyu2008] Copy to clipboard
Posted by: andy_wang_5
Posted on: 2008-06-30 09:21

这是值传递和引用传递的问题

10.Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re: andy_wang_5] Copy to clipboard
Posted by: jancyu2008
Posted on: 2008-06-30 23:03

andy_wang_5 wrote:
这是值传递和引用传递的问题

Of course.
Java was very very hard ,but also very very easy

11.Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re: jancyu2008] Copy to clipboard
Posted by: jancyu2008
Posted on: 2008-07-02 18:57

public class Foo
{public static void main(String[] sgf)
{
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operate(a,b);
System.out.println(a+","+b);//这里要求不能有变动
}
static void operate(StringBuffer x,StringBuffer y)
{
x.append(y);
y=new (x.toString());
}
}


这个也可以达到效果


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923