• Java 拷贝方式


    Java 有哪几种拷贝方式,哪种拷贝效率高

    数组拷贝

       @Test
        public void ArrayClone() {
            
            //普通for循环复制数组
            int[] oldArray = {1,2,3,4,5,6,7,8,9,10};
            int[] newArray = new int[oldArray.length];
            for(int i=0; i<oldArray.length; i++) {
                newArray[i]=oldArray[i];
            }
            System.out.println(Arrays.toString(newArray));
            
            //System.arraycopy
            int[] intArray = new int[oldArray.length];
            System.arraycopy(oldArray, 0, intArray, 0, oldArray.length);
            System.out.println(Arrays.toString(intArray));
            
            //Arrays.copyOf 方法实现也是调用 System.arraycopy
            int[] array = new int[oldArray.length];
            array = Arrays.copyOf(oldArray, oldArray.length);
            System.out.println(Arrays.toString(array));
            
            //clone 对于基本数据类型clone是浅拷贝
            int[] nums = new int[oldArray.length];
            nums = oldArray.clone();
            System.out.println(Arrays.toString(nums));
        }

    对象拷贝

        @Test
        public void objectClone() throws CloneNotSupportedException, IllegalAccessException, InvocationTargetException {
            Person tom = new Person("Tom", 12);
            
            //引用拷贝
            Person mark = tom;
            System.out.println(mark.getName() + " " + mark.getAge());
            
            //clone
            Person xiaoming = (Person) tom.clone();
            tom.setAge(23);
            System.out.println(xiaoming.getName() + " " + xiaoming.getAge());
            
            //Student类必须是public修饰的
            //BeanUtils.copyProperties
            Student student = new Student("小明", "12345", 24);
            Student lilei = new Student();
            BeanUtils.copyProperties(lilei, student);
            System.out.println(lilei.getName() + "  " + lilei.getNo() + "  " + lilei.getAge());
            
            //Map转对象
            Map<String, Object> map = new HashMap<>();
            map.put("name", "小刚");
            map.put("no", "110");
            map.put("age", 100);
            
            Student xiaogang = new Student();
            BeanUtils.copyProperties(xiaogang, map);
            System.out.println(xiaogang.getName() + "  " + xiaogang.getNo() + "  " + xiaogang.getAge());
        }
    public class Person implements Cloneable {
        
        private String name;
        private Integer age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public Person(String name, Integer age) {
            super();
            this.name = name;
            this.age = age;
        }
        public Person() {}
        
        //实现clone方法
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
        
    }
    import java.io.Serializable;
    
    public class Student implements Serializable {
        private static final long serialVersionUID = 1L;
        private String name;
        private String no;
        private Integer age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getNo() {
            return no;
        }
        public void setNo(String no) {
            this.no = no;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public Student(String name, String no, Integer age) {
            super();
            this.name = name;
            this.no = no;
            this.age = age;
        }
        public Student() {}
        @Override
        public String toString() {
            return "Student [name=" + name + ", no=" + no + ", age=" + age + "]";
        }
        
    }

    实现Serializable接口实现对象拷贝

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import www.maxinhai.com.Student;
    
    public class Teacher implements Serializable {
        
        private static final long serialVersionUID = 1L;
        private String name;
        private Student student;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Student getStudent() {
            return student;
        }
        public void setStudent(Student student) {
            this.student = student;
        }
        public Object deepCopy() throws ClassNotFoundException, IOException {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream  oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            //将当前这个对象写到一个输出流当中,,因为这个对象的类实现了Serializable这个接口,所以在这个类中
            //有一个引用,这个引用如果实现了序列化,那么这个也会写到这个输出流当中
            
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            return ois.readObject();
            //这个就是将流中的东西读出类,读到一个对象流当中,这样就可以返回这两个对象的东西,实现深克隆
        }
        @Override
        public String toString() {
            return "Teacher [name=" + name + ", student=" + student + "]";
        }
        
    }
        @Test
        public void testSerializable() throws ClassNotFoundException, IOException {
            Teacher teacher = new Teacher();
            teacher.setName("李老师");
            Student student = new Student("小明", "12345", 24);
            teacher.setStudent(student);
            
            Teacher teacherLi = new Teacher();
            teacherLi = (Teacher) teacher.deepCopy();
            teacher.setName("teacher Li");
            System.out.println(teacherLi.toString());
        }
  • 相关阅读:
    使用HSQLDB 客户端(jvm自带数据库使用技巧)
    PageHelper分页
    使用exe4j打包Java程序
    有图形界面的聊天程序
    同时收发消息
    服务端和客户端互聊
    使用Scanner
    收发字符串
    收发数字
    建立连接
  • 原文地址:https://www.cnblogs.com/mxh-java/p/12452145.html
Copyright © 2020-2023  润新知