Tuesday, 24 October 2017

How to get shallow copy of array?

clone() method of array return the shallow copy of this array.

Test.java
public class Test {

 public static void printArray(int[] arr) {
  for (int data : arr) {
   System.out.print(data + " ");
  }
 }

 public static void main(String args[]) {
  int[] arr1 = { 2, 3, 5, 7 };
  int[] arr2 = arr1.clone();

  System.out.println("Elements in arr1 are :");
  printArray(arr1);

  System.out.println("\nElements in arr2 are :");
  printArray(arr2);
 }
}


How shallow copy works on reference types?
A shallow copy just copies the values of the references. Lets see it by an example.


Lets take a Student class, which contains 'marks' object and two fields(name, id).
If we clone Student s1 as Student s2. Then fields other than reference variables are copied as separate variables, but the reference variable 'marks' is same for both student s1 and s2. I.e, a shallow copy just copies the values of the references in the class. So changes to the marks object by student s1 is reflected to s2 and vice versa.
Find the below working application.

Marks.java
package com.sample.model;

public class Marks {
 public int maths, physics, chemistry;

 public Marks(int m, int p, int c) {
  maths = m;
  physics = p;
  chemistry = c;
 }
}

Student.java
package com.sample.model;

public class Student implements Cloneable {
 public int id;
 public String name;
 public Marks marks;

 public Student(int id, String name, Marks marks) {
  this.id = id;
  this.name = name;
  this.marks = marks;
 }

 public void printMarks() {
  System.out.println("Maths: " + marks.maths);
  System.out.println("Physics: " + marks.physics);
  System.out.println("Chemistry: " + marks.chemistry);
 }

 @Override
 public String toString() {
  return id + "." + name;
 }

 @Override
 public Object clone() throws CloneNotSupportedException {
  return super.clone();
 }
}

Test.java
package utils;

import com.sample.model.Marks;
import com.sample.model.Student;

public class Test {

 public static void main(String args[]) throws CloneNotSupportedException {
  Marks m1 = new Marks(95, 96, 75);

  Student s1 = new Student(1, "Krishna", m1);
  Student s2 = (Student) s1.clone();

  System.out.println("For student s1");
  System.out.println(s1);
  s1.printMarks();

  System.out.println("\n\nFor student s2");
  System.out.println(s2);
  s2.printMarks();

  System.out.println("\nchange marks of student s2 to 35, 45, and 55");
  s2.marks.maths = 35;
  s2.marks.physics = 45;
  s2.marks.chemistry = 55;

  System.out.println("Change the name to hari and id to 2 for student s2");
  s2.name = "Hari";
  s2.id = 2;

  System.out.println("\n\nFor student s1");
  System.out.println(s1);
  s1.printMarks();

  System.out.println("\n\nFor student s2");
  System.out.println(s2);
  s2.printMarks();
 }
}

Output
For student s1
1.Krishna
Maths: 95
Physics: 96
Chemistry: 75


For student s2
1.Krishna
Maths: 95
Physics: 96
Chemistry: 75

change marks of student s2 to 35, 45, and 55
Change the name to hari and id to 2 for student s2


For student s1
1.Krishna
Maths: 35
Physics: 45
Chemistry: 55


For student s2
2.Hari
Maths: 35
Physics: 45
Chemistry: 55





No comments:

Post a Comment