You can try using System.arraycopy()

int[] src  = new int[]{1,2,3,4,5};
int[] dest = new int[5];

System.arraycopy( src, 0, dest, 0, src.length );

But, probably better to use array.clone() in most cases:

int[] src = ...
int[] dest = src.clone();
Answer from Bala R on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-copyof-in-java-with-examples
Arrays copyOf() in Java with Examples - GeeksforGeeks
July 23, 2025 - The below example shows the basic use of Arrays.copyOf() method to create a copy of an array with a specified length. ... import java.util.Arrays; public class ArraysCopyOf { public static void main(String[] args) { int[] arr1 = {1, 2, 3, 4, 5}; // copy the array to a new array with the same length int[] arr2 = Arrays.copyOf(arr1, arr1.length); System.out.println("Original Array: " + Arrays.toString(arr1)); System.out.println("Copied Array: " + Arrays.toString(arr2)); } }
🌐
DataCamp
datacamp.com › doc › java › copyof
Java Array copyOf()
Here, Arrays.copyOf() is used to create a new array extendedArray with a length of 5. The additional elements are initialized to the default value of 0 for the int type. import java.util.Arrays; public class CopyOfSmallerExample { public static void main(String[] args) { String[] originalArray ...
🌐
TutorialsPoint
tutorialspoint.com › home › java/util › java arrays copyof method
Java Arrays copyOf Method
September 1, 2008 - The Java Arrays copyOf(int[] original,int newLength) method copies the specified array, truncating or padding with false (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, ...
🌐
CodeGym
codegym.cc › java blog › java arrays › arrays.copyof() method in java
Arrays.copyOf() Method in Java
December 24, 2024 - import java.util.Arrays; public class ArraysCopyOfMethod { public static void main(String[] args) { // Initialize your templateArray which you want a copy of int[] templateArray = new int[] {1, 2, 3, 4, 5, 6}; System.out.println("Template Array: " + Arrays.toString(templateArray)); // Create a "copy" of template array using // Arrays.copyOf(int[] array, int arrayLength) method // CASE 1: Sizes of both template & copy arrays are same int[] copyArray1 = Arrays.copyOf(templateArray, templateArray.length); System.out.println("Copy Array 1: " + Arrays.toString(copyArray1)); // CASE 2: Size of copy
🌐
Medium
medium.com › @AlexanderObregon › javas-arrays-copyof-method-explained-a5d53c97272d
Java’s Arrays.copyOf() Method Explained | Medium
August 24, 2024 - The Arrays.copyOf() method is part of the java.util.Arrays class and provides a easily way to copy elements from an existing array into a new array. Syntax: public static <T> T[] copyOf(T[] original, int newLength) This method creates a new ...
🌐
Baeldung
baeldung.com › home › java › java array › how to copy an array in java
How to Copy an Array in Java | Baeldung
May 11, 2024 - Let’s look at copyOf first: int[] array = {23, 43, 55, 12}; int newLength = array.length; int[] copiedArray = Arrays.copyOf(array, newLength); It’s important to note that the Arrays class uses Math.min(…) for selecting the minimum of the source array length, and the value of the new length parameter to determine the size of the resulting array.
🌐
GeeksforGeeks
geeksforgeeks.org › java › array-copy-in-java
Array Copy in Java - GeeksforGeeks
July 23, 2025 - We can use Arrays.copyOf() method, if we want to copy the first few elements of an array or make a full copy of the array, we can use this method. ... // Java program to demonstrate array // copy using Arrays.copyOf() import java.util.Arrays; ...
Find elsewhere
🌐
Tutorial Gateway
tutorialgateway.org › java-arrays-copyof-method
Java Arrays.copyOf Method
March 25, 2025 - In this Java program, we declared the floating-point array with random elements. Then we call the public static float copyOf (float[] anFloatArray, int newLength) to copy the float array to a new one of a specified length.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java copy array: how to copy / clone an array in java
Java Copy Array: How To Copy / Clone An Array In Java
April 1, 2025 - In the above program, we copy the even_Array of length 4 by using the copyOf method. The second argument provided is 5. Hence, the new copied array has 5 elements in it.
🌐
DEV Community
dev.to › luthfisauqi17 › multiple-ways-to-copy-array-in-java-effectively-4lpj
Multiple Ways to Copy Array in Java Effectively - DEV Community
September 17, 2022 - int[] a = {1, 2, 3, 4, 5}; int[] b = Arrays.copyOf(a, a.length); a[0]++; System.out.println("Array a: " + Arrays.toString(a)); System.out.println("Array b: " + Arrays.toString(b)); ... If you are interested in this method, and want to learn ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › Arrays.html
Arrays (Java Platform SE 7 )
ArrayStoreException - if an element ... · public static byte[] copyOf(byte[] original, int newLength) Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length....
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.arrays.copyof
Arrays.CopyOf Method (Java.Util) | Microsoft Learn
Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. [Android.Runtime.Register("copyOf", "([Ljava/lang/Object;I)[Ljava/lang/Object;", "")] [Java.Interop.JavaTypeParameters(new ...
Top answer
1 of 4
2

The Arrays.copyOf(arr1, i + incrementLength); in your code is creating unnecessary multiple copies of arr1 inside the loop. Something that you should avoid doing.

Take a look at this Documentation from Oracle. The second parameter is the number of elements from arr1 that you will be copying to arr2.

Instead, you can add the needed elements to a List and obtain a copy of the array from that list. Something like this:

Copy    List<String> list = new ArrayList<>();

    for (int i = 0; i < arr1.length; i++) {
        if(!arr1[i].equals("a") && !arr1[i].equals("c")) {
            list.add(arr[i]);
        }
    }

    arr2 = list.toArray(new String[list.size()]);

Update

If you do not wish to use Lists, you can try this:

Copy    String[] arr1 = new String[] { "a", "b", "c", "d", "e" };
    String[] arr2 = new String[arr1.length];
    int j = 0;
    int finalLength = 0;
    for (int i = 0; i < arr1.length; i++) {
        if(!arr1[i].equals("a") && !arr1[i].equals("c")) {
            // add only elements that you need
            arr2[j++] = arr1[i];
            // keep track of how many elements you have added
            finalLength++;
        }
    }

    // truncate array to finalLength
    String[] arr3 = Arrays.copyOf(arr2, finalLength);
    for (String value : arr3) {
        System.out.println(value);
    }

This generates the following output:

Copy    b
    d
    e

Hope this helps!

2 of 4
1

If you want to copy individual elements of the source array, you can't use Arrays.copyOf. Had you copied a continuous sub-range of the array, you could have used System.arraycopy, but that's not what you are doing. Arrays.copyOf can only be used to either copy the entire array or a prefix of the array (i.e. all the elements from the first element until some index).

You have to first calculate the required length of the output array, instantiate the output array and then iterate over the elements of the input array and copy the relevant elements one by one to the output array.

Copypublic static void main(String[] args) {
    String[] arr1 = new String[] { "a", "b", "c", "d", "e" };
    int newLen = 0;
    for (int i = 0; i < arr1.length; i++) {
        if(!arr1[i].equals("a") && !arr1[i].equals("c")) {
            newLen++;
        }
    }
    String[] arr2 = new String[newLen];
    int index = 0;
    for (int i = 0; i < arr1.length; i++) {
        if(!arr1[i].equals("a") && !arr1[i].equals("c")) {
            arr2[index++] = arr1[i];
        }
    }
    for (String value : arr2) {
        System.out.println(value);
    }
}

This has a disadvantage of iterating over the input array twice, since you can't create the output array until you know how many elements it should contain. You can avoid that by adding the selected elements of the input array to an ArrayList<String>, and then convert the ArrayList to an array.

🌐
Baeldung
baeldung.com › home › java › java array › performance of system.arraycopy() vs. arrays.copyof()
Performance of System.arraycopy() vs. Arrays.copyOf() | Baeldung
January 8, 2024 - Arrays.copyOf() offers additional functionality on top of what System.arraycopy() implements. While System.arraycopy() simply copies values from the source array to the destination, Arrays.copyOf() also creates new array.
🌐
Java67
java67.com › 2018 › 02 › how-to-copy-elements-of-one-array-to-other-java-example.html
How to copy Array in Java? Arrays copyOf and copyOfRange Example | Java67
How to Search an Element in Java Array with Exampl... [Solved] How to solve a coin change problem in Jav... How to print a Right Triangle Pattern in Java - Ex...
🌐
Coding Shuttle
codingshuttle.com › java-programming-handbook › java-copy-arrays
Java Copy Arrays | Coding Shuttle
April 9, 2025 - The Arrays.copyOf() method is a simple way to copy an array while also allowing us to change its size. import java.util.Arrays; class Main { public static void main(String[] args) { int[] original = {5, 10, 15, 20, 25}; // Copying entire array ...
🌐
CodeJava
codejava.net › java-core › collections › copying-and-filling-arrays-in-java
Copying and Filling Arrays in Java
String[] original = {"Apple", "Banana", "Carrot", "Lemon", "Orange", "Grape"}; String[] copy1 = Arrays.copyOf(original, original.length); String[] copy2 = Arrays.copyOf(original, 3); String[] copy3 = Arrays.copyOf(original, 10); System.out.println("original array: " + Arrays.toString(original)); System.out.println("copy #1 (same length): " + Arrays.toString(copy1)); System.out.println("copy #2 (truncated): " + Arrays.toString(copy2)); System.out.println("copy #3 (padded): " + Arrays.toString(copy3));Output: original array: [Apple, Banana, Carrot, Lemon, Orange, Grape] copy #1 (same length): [Apple, Banana, Carrot, Lemon, Orange, Grape] copy #2 (truncated): [Apple, Banana, Carrot] copy #3 (padded): [Apple, Banana, Carrot, Lemon, Orange, Grape, null, null, null, null]Here, note that the padded elements are initialized to nulls (for object reference types).
🌐
Studytonight
studytonight.com › java-util › java-arrays-copyof-method
Java Arrays copyOf() Method - Studytonight
It indicates that we can create a new array of different sizes from an existing array using copyOf() method. import java.util.Arrays; public class StudyTonight { public static void main(String[] args) { int[] arr = new int[] {5, 6, 7, 10, 17}; ...