Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 384|回复: 0

[Java基础知识]在java程序中交换对象

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-9-30 17:48:15 | 显示全部楼层 |阅读模式

    1. 1. 概述
    2. 在java中,参数传递是通过传值(对于对象类型,传递的是引用的拷贝),所以常规的交换实现不了两个对象的交换,下面是测试代码:

    3. Java代码  
    4. import org.apache.commons.beanutils.BeanUtils;   
    5.   
    6. public class Employee {   
    7.     // Properties   
    8.     private int id;   
    9.     private String name;   
    10.       
    11.     // Constructors   
    12.     public Employee() {   
    13.     }   
    14.       
    15.     public Employee(int id, String name) {   
    16.         this.id = id;   
    17.         this.name = name;   
    18.     }   
    19.       
    20.     // get、set methods   
    21.     public int getId() {   
    22.         return id;   
    23.     }   
    24.   
    25.     public void setId(int id) {   
    26.         this.id = id;   
    27.     }   
    28.   
    29.     public String getName() {   
    30.         return name;   
    31.     }   
    32.   
    33.     public void setName(String name) {   
    34.         this.name = name;   
    35.     }   
    36.       
    37.     // 无效的交换   
    38.     public static void swap(Employee x, Employee y) {   
    39.         Employee temp = x;   
    40.         x = y;   
    41.         y = temp;   
    42.     }   
    43.       
    44.     // 使用 BeanUtils 方法的有效交换   
    45.     public static void swapByBeanUtils(Employee x, Employee y) {   
    46.         try {   
    47.             // clone x to temp   
    48.             Employee temp = (Employee)BeanUtils.cloneBean(x);   
    49.             // copyProperties from y to x   
    50.             BeanUtils.copyProperties(x, y);   
    51.             // copyProperties from temp to y   
    52.             BeanUtils.copyProperties(y, temp);   
    53.             // then, the Properties values of x,y has been swaped.   
    54.         } catch (Exception e) {   
    55.             e.printStackTrace();   
    56.         }   
    57.     }   
    58.       
    59.     // 有人提出使用交换数组等集合里的元素 来实现交换,这样也是无效的   
    60.     public static void swapByArray(Object[] a, int i, int j) {   
    61.         Object t = a[i];   
    62.         a[i] = a[j];   
    63.         a[j] = t;   
    64.     }   
    65.       
    66.     public static void main(String[] args) {   
    67.         Employee a = new Employee(1, "Bob");   
    68.         Employee b = new Employee(2, "Jack");   
    69.            
    70.         // 直接方法调用交换 -- NO   
    71. //      swap(a, b);   
    72.            
    73.         // 利用BeanUtils交换 -- YES   
    74. //      swapByBeanUtils(a, b);   
    75.            
    76.         // 利用 交换数据 思想 -- NO   
    77. //      Object[] o = {a, b};   
    78. //      swapByArray(o, 0, 1);   
    79.            
    80.         // 直接交换 -- YES   
    81. //      Employee temp = a;   
    82. //      a = b;   
    83. //      b = temp;   
    84.            
    85.         System.out.println(a);   
    86.         System.out.println(b);         
    87.     }   
    88.       
    89.     // method for print   
    90.     public String toString() {   
    91.         StringBuilder sb = new StringBuilder();   
    92.         sb.append("id: ").append(id).append(" name: ").append(name);   
    93.         return sb.toString();   
    94.     }   
    95. }  
    96. import org.apache.commons.beanutils.BeanUtils;
    97. public class Employee {
    98.         // Properties
    99.         private int id;
    100.         private String name;
    101.        
    102.         // Constructors
    103.         public Employee() {
    104.         }
    105.        
    106.         public Employee(int id, String name) {
    107.                 this.id = id;
    108.                 this.name = name;
    109.         }
    110.        
    111.         // get、set methods
    112.         public int getId() {
    113.                 return id;
    114.         }
    115.         public void setId(int id) {
    116.                 this.id = id;
    117.         }
    118.         public String getName() {
    119.                 return name;
    120.         }
    121.         public void setName(String name) {
    122.                 this.name = name;
    123.         }
    124.        
    125.         // 无效的交换
    126.         public static void swap(Employee x, Employee y) {
    127.                 Employee temp = x;
    128.                 x = y;
    129.                 y = temp;
    130.         }
    131.        
    132.         // 使用 BeanUtils 方法的有效交换
    133.         public static void swapByBeanUtils(Employee x, Employee y) {
    134.                 try {
    135.                         // clone x to temp
    136.                         Employee temp = (Employee)BeanUtils.cloneBean(x);
    137.                         // copyProperties from y to x
    138.                         BeanUtils.copyProperties(x, y);
    139.                         // copyProperties from temp to y
    140.                         BeanUtils.copyProperties(y, temp);
    141.                         // then, the Properties values of x,y has been swaped.
    142.                 } catch (Exception e) {
    143.                         e.printStackTrace();
    144.                 }
    145.         }
    146.        
    147.         // 有人提出使用交换数组等集合里的元素 来实现交换,这样也是无效的
    148.         public static void swapByArray(Object[] a, int i, int j) {
    149.             Object t = a[i];
    150.             a[i] = a[j];
    151.             a[j] = t;
    152.         }
    153.        
    154.         public static void main(String[] args) {
    155.                 Employee a = new Employee(1, "Bob");
    156.                 Employee b = new Employee(2, "Jack");
    157.                
    158.                 // 直接方法调用交换 -- NO
    159. //                swap(a, b);
    160.                
    161.                 // 利用BeanUtils交换 -- YES
    162. //                swapByBeanUtils(a, b);
    163.                
    164.                 // 利用 交换数据 思想 -- NO
    165. //                Object[] o = {a, b};
    166. //                swapByArray(o, 0, 1);
    167.                
    168.                 // 直接交换 -- YES
    169. //                Employee temp = a;
    170. //                a = b;
    171. //                b = temp;
    172.                
    173.                 System.out.println(a);
    174.                 System.out.println(b);               
    175.         }
    176.        
    177.         // method for print
    178.         public String toString() {
    179.                 StringBuilder sb = new StringBuilder();
    180.                 sb.append("id: ").append(id).append(" name: ").append(name);
    181.                 return sb.toString();
    182.         }
    183. }
    184. 2. 结论
    185. 在java中,对象引用是通过值来传递的,方法不能修改参数指向新的对象。

    186. 注:需加载 BeanUtils包
    复制代码
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-9 11:59 , Processed in 0.434153 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表