HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Java Array Copy Example

By Lokesh Gupta | Filed Under: Java Array

Java examples to copy an array to another array object using array clone, System.arrayCopy() and Arrays.copyOf() methods.

Note that all given below approaches create shallow copy of array. To create deep copies of array, follow provided link.

Quick Reference:

String[] names = {"Alex", "Brian", "Charles", "David"};
		
// Use arr.clone() method - Recommended
String[] cloneOfNames = names.clone();

// Use Arrays.copyOf() method - Most readable
String[] copyOfNames = Arrays.copyOf(names, names.length);

//Using System.arraycopy() method - Equally efficient but less readable
String[] copyOfNames2 = new String[names.length];
System.arraycopy(names, 0, copyOfNames2, 0, copyOfNames2.length);

1) array.clone()

It is the most easy to use and recommended approach. It create the clone of array in single statement. Most developers are well aware of clone() functionality in general, so they can relate easily.

String[] names = {"Alex", "Brian", "Charles", "David"};
		
// Use arr.clone() method - Recommended
String[] cloneOfNames = names.clone();

System.out.println(Arrays.toString(names));			//[Alex, Brian, Charles, David]
System.out.println(Arrays.toString(cloneOfNames)); 	//[Alex, Brian, Charles, David]

Read More: Print Array Content

2) Arrays.copyOf()

This is super useful method and very clearly state it’s purpose. Make the code very much readable. I will recommend this as well.

String[] names = {"Alex", "Brian", "Charles", "David"};
		
// Use Arrays.copyOf() method - Most readable
String[] copyOfNames = Arrays.copyOf(names, names.length);

System.out.println(Arrays.toString(names));			//[Alex, Brian, Charles, David]
System.out.println(Arrays.toString(copyOfNames)); 	//[Alex, Brian, Charles, David]

3) System.arraycopy() – Recommended

Another useful method provided by JDK. It is native method and is also invoked within Arrays.copyOf() method, internally. But it makes code a little less readable due to multiple arguments used for invocation.

/** 
* @param      src      the source array.
* @param      srcPos   starting position in the source array.
* @param      dest     the destination array.
* @param      destPos  starting position in the destination data.
* @param      length   the number of array elements to be copied.
*/
public static native void arraycopy(Object src,  int  srcPos,
									Object dest, int destPos,
                                    int length);

Let’s see the example of arraycopy method.

String[] names = {"Alex", "Brian", "Charles", "David"};
		
//Using System.arraycopy() method - Equally efficient but less readable
String[] copyOfNames = new String[names.length];
System.arraycopy(names, 0, copyOfNames, 0, copyOfNames.length);

System.out.println(Arrays.toString(names));			//[Alex, Brian, Charles, David]
System.out.println(Arrays.toString(copyOfNames)); 	//[Alex, Brian, Charles, David]

Use any of above method to copy array in java. Please note that above method will create the shallow copy of array.

Happy Learning !!

References:

System.arraycopy()
Arrays.copyOf()
Java clone() method

Sourcecode Download

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Leave a Reply

This comment form is under antispam protection
This comment form is under antispam protection
  Subscribe  
Notify of

Search Tutorials

Java Array

  • Java – Array Introduction
  • Java – Print Array
  • Java – Print 2D Array
  • Java – Copy Array
  • Java – Copy Array Range
  • Java – Clone Array
  • Java – Array Deep Copy
  • Java – String to String[]
  • Java – byte[] to String
  • Java – String to byte[]
  • Java – Array Union
  • Java – Array Intersection
  • Array – Remove duplicate elements

Java Collections

  • Collections Framework
  • Array
  • ArrayList
  • LinkedList
  • HashMap
  • Hashtable
  • LinkedHashMap
  • TreeMap
  • HashSet
  • LinkedHashSet
  • TreeSet
  • Comparable
  • Comparator
  • Iterator
  • ListIterator
  • Spliterator
  • PriorityQueue
  • PriorityBlockingQueue
  • ArrayBlockingQueue
  • LinkedTransferQueue
  • CopyOnWriteArrayList
  • CopyOnWriteArraySet
  • Collection Sorting
  • Interview Questions

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz