February 23, 2019

How to copy array in Java best example source code

Copy an array into another in java

You can either use any of this following methods only for one dimensional array


  1. Object.clone()
  2. System.arraycopy()
  3. Arrays.copyOf()
  4. Arrays.copyOfRange()
  5. ArrayUtils.clone()  from  apache commons

Java code

/**
 *  Copying an array in Java
 *
 */
package com.Konzerntech.tutorial;

import java.util.Arrays;

/**
 * @author Konzerntechnologies
 *
 */
public class CopingArray {

/**
* @param args
*/
public static void main(String[] args) {

// Source array
int a[] = {8,2,8,1,8,0,8,0,2,9};


/**
* Since array is also an Object
* you can use clone method
* you can copy the whole object
* But partial copying is not possible
*/
int d[] = a.clone();
System.out.println(" using clone method ");
for(int i =0; i< d.length; i++) {
System.out.print(d[i]+"\t");
}


/**
* The best way to copy array
* you can do partial copying
* From a source to destination to a different position
*
* The below code will copy 4 elements from
* starting from first arrays second position to second arrays 3rd position
*/
System.out.println("\n using System.arrayCopy() method ");
int b[] = new int[a.length];
System.arraycopy(a, 2, b, 3, 4);
for(int i =0; i< b.length; i++) {
System.out.print(b[i]+"\t");
}

/**
* If you want to copy array partially or fully
* use array copy method
*/
System.out.println("\n using Arrays.copyOf() method ");
int c[] = Arrays.copyOf(a, a.length);
for(int i =0; i< c.length; i++) {
System.out.print(c[i]+"\t");
}

/**
* If you want to copy array partially or fully
* use array copy method
*/
System.out.println("\n rusing Arrays.copyOfRange() method ");
int e[] = Arrays.copyOfRange(a, 2 ,a.length);
for(int i =0; i< e.length; i++) {
System.out.print(e[i]+"\t");
}

}

}


Output


 using clone method 
8 2 8 1 8 0 8 0 2 9
 using System.arrayCopy() method 
0 0 0 8 1 8 0 0 0 0
 using Arrays.copyOf() method 
8 2 8 1 8 0 8 0 2 9
 rusing Arrays.copyOfRange() method 
8 1 8 0 8 0 2 9


Description

If you are going behind performance dont use clone method, as the clone method works on Object class, there will be internal type casting to datatypes

You can use  ArrayUtils.clone from  apache commons

int arr[] = ArrayUtils.clone(source_array);

All the methods discussed above performs shallow copy so they are suitable for immutable objects and primitive data types.

If you want to copy a mutable object go for deep copy ( Create a new memory for object while copying )


email us at itatcalicut(at)gmail(dot)com

Follow this link to join my WhatsApp group: https://chat.whatsapp.com/HR0H7MG7AeA2PMApmfEeZn

February 18, 2019

Method chaining example with thread example

/**
 *  you can invoke multiple methods in an object to create a object using method chaining
 * 
 */
package com.konzerntech.pattern;

/**
 * @author konzerntech
 *
 */
public class Hotel {

private int hotelId;
private String hotelName;
private String hotelDescription;

public Hotel setHotelId(int hotelId) {
this.hotelId = hotelId;
return this;
}

public Hotel setHotelName(String hotelName) {
this.hotelName = hotelName;
return this;
}

public Hotel setHotelDescription(String hotelDescription) {
this.hotelDescription = hotelDescription;
return this;
}


@Override
public String toString() {
return hotelId +" @ "+hotelName +" @ "+hotelDescription;
}
}



/**
 * 
 */
package com.konzerntech.pattern;

/**
 * @author Apple
 *
 */
public class HotelThread implements Runnable {
private final Hotel hotel = new Hotel();
private int id;
private String name;
private String desc;
public Hotel getHotel() {
return hotel;
}
public HotelThread(int hotelId, String name, String desc) {
this.id = hotelId;
this.name = name;
this.desc = desc;
}

@Override
public void run() {
hotel.setHotelId(id).setHotelName(name).setHotelDescription(desc);
}

}
/**
 * 
 */
package com.konzerntech.pattern;

/**
 * @author Apple
 *
 */
public class MainClass {

/**
* @param args
*/
public static void main(String[] args) {

HotelThread hotel1 = new HotelThread(1, "hyatt", "diear");
HotelThread hotel2 = new HotelThread(2, "burj al arab", "marina");
HotelThread hotel3 = new HotelThread(3, "jw mariot", "dubai");
Thread t1 = new Thread(hotel1);
Thread t2 = new Thread(hotel2);
Thread t3 = new Thread(hotel3);
t1.start();
t2.start();
t3.start();
System.out.println(hotel1.getHotel());
System.out.println(hotel2.getHotel());
System.out.println(hotel3.getHotel());
}

}

Facebook comments