java中swap函数的实现分析

本文探讨了Java中如何实现swap操作,介绍了适用于不同场景的三种方法:通过数组、自定义包装类以及外部内联方式,并分析了String和引用类型的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实践是检验真理的唯一标准,java代码:

public class TestReference {
	public static void main(String[] args) {
        String b = "hi";  
        String a = "hello";  
        a = b;  
        b = "love";
        System.out.println("a: " + a);
//        String类型与其他类型不同,它初始化后不会改变,只能重新生成一个新的String变量
//        ==========================================================
        Test test = new Test();
        test.setId(1);
        Test test2 = new Test();
        test2.setId(2);
        
        System.out.println("before swap");
        System.out.println(test);
        System.out.println(test2);
        
        swap(test, test2);
        
//        Test test3 = null;
//        test3 = test;
//        test = test2;
//        test2 = test3;
        
        System.out.println("after swap");
        System.out.println(test);
        System.out.println(test2);
        
	}
	
	public static void swap(Test a , Test b) {
		Test temp;
		temp = a;
		a = b;
		b = temp;
	}
}

class Test{
	private int id;
	public void setId(int id) {
		this.id = id;
	}
	public int getId(){
		return id;
	}
//	public String toString(){
//		return id + "";
//	}
}


输出结果:
a: hi
before swap
Test@c17164
Test@1fb8ee3
after swap
Test@c17164
Test@1fb8ee3


得出结论:
1、String类型与其他类型不同,它初始化后不会改变,只能重新生成一个新的String变量。b = "love";等价于 b= new String(“love”);
2、对于数组中的元素交换,可以使用下文中的public static void swap(int[] data, int a, int b)方法;
对于非数组中的元素交换,可以使用最原始的交换方法,可以实现交换;也可以使用下文中包装类的方式外部内联的方式,视情况而定

3、引用 http://www.iteye.com/topic/4189中多人赞同的一句话: java传递是引用拷贝,既不是引用本身,更不是对象。  可以帮助理解输出结果的原因,就本例而言 个人理解为:在交换过程中,test 和 test2 作为引用,其值是两个地址,与int类型无异,故使用 public static void swap(Test a , Test b) ,与使用 public static void swap(int a , int b) 一样,只是副本的交换,原变量依然未实现交换。


下面是引用高玩的帖子
=================================================================================================================================

是个程序员都知道,在C/C++里面交换值的方法:

  1. void swap(int&a ,int&b)  
  2. {  
  3.     int temp;  
  4.     temp = a;  
  5.     a = b;  
  6.     b = temp;  
  7. }  

但在JAVA中用这种方法是行不通的,因为“Java对普通类型的变量是不支持引用传递的”。

怎么办呢?

 

1. 可以像下面这样通过传数组(也属于传值)的方法来完成对换(在很多排序算法里面就是这么干的):

[java]  view plain copy print ?
  1. public static void swap(int[] data, int a, int b) {  
  2.         int t = data[a];  
  3.         data[a] = data[b];  
  4.         data[b] = t;  
  5.     }  

 

2. 也可以通过重新定义个类(在JAVA中我们可以通过使用int的包装类---Integer,然后将其作为值的引用传到函数中,但这个Integer包装类也不允许你来改变它的数据域;但这不防碍我们用自己的包装类,比如说下面实现的MyInteger):

[java]  view plain copy print ?
  1. //MyInteger: 与Integer有些类似,但是其对象可以变值  
  2. class MyInteger {     
  3.     private int x;    // 将x作为唯一的数据成员   
  4.     public MyInteger(int xIn) { x = xIn; } // 构造器   
  5.     public int getValue() { return x; }  // 得到值    
  6.     public void insertValue(int xIn) { x = xIn;} // 改变值  
  7. }  
  8.   
  9. public class Swapping {     
  10.     // swap: 传对象引用   
  11.     static void swap(MyInteger rWrap, MyInteger sWrap) {        
  12.         // 变值过程       
  13.         int t = rWrap.getValue();        
  14.         rWrap.insertValue(sWrap.getValue());        
  15.         sWrap.insertValue(t);     
  16.     }     
  17.     public static void main(String[] args) {        
  18.         int a = 23, b = 47;        
  19.         System.out.println("Before. a:" + a + ", b: " + b);        
  20.         MyInteger aWrap = new MyInteger(a);        
  21.         MyInteger bWrap = new MyInteger(b);        
  22.         swap(aWrap, bWrap);        
  23.         a = aWrap.getValue();        
  24.         b = bWrap.getValue();        
  25.         System.out.println("After.  a:" + a + ", b: " + b);     
  26.     }  
  27. }  

 

3. 由于java 中的参数传递都是采用的值传递方式,这不防碍我们用swap的时候采用外部内联的方式

[java]  view plain copy print ?
  1. public class Swap2 {   
  2.     public static void main(String args[]){   
  3.         Swap2 sw = new Swap2(1,2);   
  4.         System.out.println("i is" + sw.i);   
  5.         System.out.println("j is" + sw.j);   
  6.         sw.swap();   
  7.         System.out.println("i is" + sw.i);   
  8.         System.out.println("j is" + sw.j);   
  9.     }   
  10.     int i,j;   
  11.     public Swap2(int i, int j){   
  12.         this.i = i;   
  13.         this.j = j;   
  14.     }   
  15.   
  16.     public  void swap(){   
  17.         int temp;   
  18.         temp = i;   
  19.         i = j;   
  20.         j = temp;   
  21.     }   
  22. }   
  23.   
  24. public class Swap1 {   
  25.     public static void Swap1(Integer a, Integer b){   
  26.         Integer temp = a;   
  27.         a = b;   
  28.         b = temp;   
  29.     }   
  30.     public  static  void main(String args[]){   
  31.         Integer a,b;   
  32.         a = new Integer(10);   
  33.         b = new Integer(20);   
  34.         Swap1.Swap1(a, b);   
  35.         System.out.println("a is " + a);   
  36.         System.out.println("b is " + b);   
  37.     }   
  38. }  
    ====================================================================================================== 
    Swap1 结果:
    a is 10
    b is 20
    Swap2 结果:
    i is1
    j is2
    i is2
    j is1

 

普及知识:

Java是传值还是传引用:http://www.bccn.net/Article/kfyy/java/jszl/200601/3069.html

在C/C++/JAVA中实现swap:http://www.cs.utsa.edu/~wagner/CS2213/swap/swap.html

 

 

 

参考:

http://wzdoxu.iteye.com/blog/251988

http://hi.baidu.com/aleczhou/blog/item/975ad262391313d9e7113aa0.html

原文:http://blog.youkuaiyun.com/dadoneo/article/details/6577976

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值