+ -
当前位置:首页 → 问答吧 → [新手上路]一D简单JAVA问题

[新手上路]一D简单JAVA问题

时间:2014-04-04

来源:互联网

我想问点样改先可以令num1同num2 swap到???thanks

public class TestSwap {


public static void main(String[] argv) {
int num1=1;
int num2=2;

System.out.println("num1 is " + num1 + " and num2 is " + num2);
swap(num1, num2);
System.out.println("num1 is " + num1 + " and num2 is " + num2);
}

public static void swap(int n1, int n2) {

int temp = n1;
n1 = n2;
n2 = temp;
}
}

作者: fabregasfans   发布时间: 2014-04-04

请问点解要咁做?

作者: a8d7e8   发布时间: 2014-04-04

唔系已经swap 咗呢??

作者: me888   发布时间: 2014-04-04

引用:原帖由 me888 於 2014-3-12 06:17 PM 发表
唔系已经swap 咗呢??



冇SWAP到
出黎结果2句都系num1=1 同埋num2 =2

作者: fabregasfans   发布时间: 2014-04-04

虽然汪汪唔识Java
不过以c++的角度看

楼主未识分call by value和call by reference的分别

作者: Susan﹏汪汪   发布时间: 2014-04-04

差唔多十几年无写prog, 汪汪讲得啱

网上抄咗段解释

Say I want to share a web page with you.

If I tell you the URL, I'm passing by reference. You can use that URL to see the same web page I can see. If that page is changed, we both see the changes. If you delete the URL, all you're doing is destroying your reference to that page - you're not deleting the actual page itself.

If I print out the page and give you the printout, I'm passing by value. Your page is a disconnected copy of the original. You won't see any subsequent changes, and any changes that you make (e.g. scribbling on your printout) will not show up on the original page. If you destroy the printout, you have actually destroyed your copy of the object - but the original web page remains intact.

作者: me888   发布时间: 2014-04-04

你应该 constants 同 variables 搞乱咗,Java syntax 有些似 Ansi C , syntax 自己改番就okay la.

作者: me888   发布时间: 2014-04-04

引用:原帖由 me888 於 2014-3-12 08:05 PM 发表
你应该 constants 同 variables 搞乱咗,Java syntax 有些似 Ansi C , syntax 自己改番就okay la.



Java只有call by value....没call by reference或者call by pointer
所以楼主的swap功能系一大挑战

作者: Susan﹏汪汪   发布时间: 2014-04-04

引用:原帖由 Susan﹏汪汪 於 2014-3-12 08:15 PM 发表
Java只有call by value....没call by reference或者call by pointer
所以楼主的swap功能系一大挑战
That's true, another limitation is the build in Integer object is immutable, that's mean that it cannot be modified after it has been created, and so the simplest way to dual with this problem is to define your own Int class, for example
复制内容到剪贴板代码:class Int2 {
int val;
}

public static void(Int2 a, Int2 b) ...

作者: assembly.jc   发布时间: 2014-04-04

可以一开头入落array?

作者: Qoo记   发布时间: 2014-04-04

我好耐冇写java,应该话好耐冇programming, 可能有错,请指教!
唔知有咩要求,所以希望岩用~
package swap;

/*
* @author IamG33k
*/
public class Swap {
private int number_one;
private int number_two;
private int number_tmp;

public static void main(String[] args) {
Swap let_swap_it = new Swap(100,356);
int num_one = let_swap_it.GetNumberOne();
int num_two = let_swap_it.GetNumberTwo();
System.out.println("a:"+ num_one +" b:" +num_two);
let_swap_it.SwapOnetoTwo();
System.out.println("a:"+ let_swap_it.number_one +" b:" +let_swap_it.number_two);

}

public Swap(int num_one, int num_two){
number_one=num_one;
number_two=num_two;
}

public void SwapOnetoTwo(){
number_tmp=number_one;
number_one=number_two;
number_two=number_tmp;
}

public int GetNumberOne(){
int num_one=number_one;
return num_one;
}

public int GetNumberTwo(){
int num_two=number_two;
return num_two;
}


}

[ 本帖最后由 IamG33k 於 2014-3-15 03:42 PM 编辑 ]

作者: IamG33k   发布时间: 2014-04-04

系mac到打中文会移位
anyway,唔一定要用get function,直接call都得,不过第时你再写多d program你就会发觉get function 有佢既用途~

作者: IamG33k   发布时间: 2014-04-04

Hi 各位
Youtube 有28堂 Standford University Lectures 由浅入深,
有兴趣嘅 programmer 可以去睇睇

Programming Methodology (Stanford)



[ 本帖最后由 me888 於 2014-3-15 03:56 PM 编辑 ]

作者: me888   发布时间: 2014-04-04

睇个website有讲
In the method swap(), we are swapping copies of theoriginal values, instead of the original values. Since localvariables and parameter variables disappear after you exit themethod, the swapped values also disappear.

http://www.cs.umd.edu/~clin/MoreJava/Container/alist-prob-swap.html

[ 本帖最后由 kelvin_0212 於 2014-3-17 09:08 AM 编辑 ]

作者: kelvin_0212   发布时间: 2014-04-04

引用:原帖由 Susan﹏汪汪 於 2014-3-12 08:15 PM 发表
Java只有call by value....没call by reference或者call by pointer
所以楼主的swap功能系一大挑战
第一次听java 没有call by reference

作者: HappyFirday   发布时间: 2014-04-04

引用:原帖由 HappyFirday 於 2014-3-21 02:18 PM 发表

第一次听java 没有call by reference



http://n2.hk/d/images/r10/mobile.jpg
同javascript 一样系call by value
Object 内部结构就用reference

作者: Susan﹏汪汪   发布时间: 2014-04-04

引用:原帖由 Susan﹏汪汪 於 2014-3-21 02:38 PM 发表

同javascript 一样系call by value
Object 内部结构就用reference



但佢咁讲法,应该系有方法call by reference,
真心 c兄写段code黎话我知点call by reference,等我学下野!

作者: IamG33k   发布时间: 2014-04-04

引用:原帖由 IamG33k 於 2014-3-21 03:36 PM 发表

但佢咁讲法,应该系有方法call by reference,
真心 c兄写段code黎话我知点call by reference,等我学下野!
除左用object做包装(或者用array、上面讲的例子)
因为java只有call by value、所以没办法做到

作者: Susan﹏汪汪   发布时间: 2014-04-04