Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 实战错误讨论  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教一个StringBuffer 问题!!!!!!!!!!!!!
jancyu2008





发贴: 36
积分: 0
于 2008-06-24 21:34 user profilesend a private message to usersend email to jancyu2008search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

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


jancyu2008 edited on 2008-06-28 00:07

Every day thinking about JAVA, because I like to
Welcome to My pagehttp://jeakylau.51.com
My E-mail:jeakylau@126.com
My QQ : 529163539
作者 Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re:jancyu2008]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-06-25 16:59 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re:jancyu2008]
jancyu2008





发贴: 36
积分: 0
于 2008-06-26 00:12 user profilesend a private message to usersend email to jancyu2008search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
謝謝版主,我也想了一個解決的辦法,不過和你的相比顯得相形見拙了,呵呵,下面是我的辦法
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);
}
}


jancyu2008 edited on 2008-06-28 00:05

Every day thinking about JAVA, because I like to
Welcome to My pagehttp://jeakylau.51.com
My E-mail:jeakylau@126.com
My QQ : 529163539
作者 Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re:jancyu2008]
jancyu2008





发贴: 36
积分: 0
于 2008-06-26 00:26 user profilesend a private message to usersend email to jancyu2008search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
今天又有了狠大的收獲


作者 Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re:jancyu2008]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-06-27 16:19 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re:jancyu2008]
jancyu2008





发贴: 36
积分: 0
于 2008-06-28 00:09 user profilesend a private message to usersend email to jancyu2008search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Ah ,I don't know what can I do it on the frist
Hehe ,thank you



Every day thinking about JAVA, because I like to
Welcome to My pagehttp://jeakylau.51.com
My E-mail:jeakylau@126.com
My QQ : 529163539
作者 Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re:HenryShanley]
Joffeec





发贴: 47
积分: 3
于 2008-06-29 11:25 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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());


Joffeec edited on 2008-06-29 11:27

作者 Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re:Joffeec]
jancyu2008





发贴: 36
积分: 0
于 2008-06-29 23:45 user profilesend a private message to usersend email to jancyu2008search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Joffeec wrote:
版主可以用一个y.delete(0,y.length());先来删掉y里面的内容再用append()的方法把x的内容加进去。。那么我用一个replace()也可以达到同样的效果。

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


Yes , Hehe ,thanks



Every day thinking about JAVA, because I like to
Welcome to My pagehttp://jeakylau.51.com
My E-mail:jeakylau@126.com
My QQ : 529163539
作者 Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re:jancyu2008]
andy_wang_5





发贴: 101
积分: 14
于 2008-06-30 09:21 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这是值传递和引用传递的问题


鹰行折翼 再盼凌云
作者 Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re:andy_wang_5]
jancyu2008





发贴: 36
积分: 0
于 2008-06-30 23:03 user profilesend a private message to usersend email to jancyu2008search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
andy_wang_5 wrote:
这是值传递和引用传递的问题

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



Every day thinking about JAVA, because I like to
Welcome to My pagehttp://jeakylau.51.com
My E-mail:jeakylau@126.com
My QQ : 529163539
作者 Re:请教一个StringBuffer 问题!!!!!!!!!!!!! [Re:jancyu2008]
jancyu2008





发贴: 36
积分: 0
于 2008-07-02 18:57 user profilesend a private message to usersend email to jancyu2008search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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());
}
}


这个也可以达到效果



Every day thinking about JAVA, because I like to
Welcome to My pagehttp://jeakylau.51.com
My E-mail:jeakylau@126.com
My QQ : 529163539

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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