Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Arrays.copyOf

Tags:

java

arrays

I want to copy only "b", "d" and "e" from arr1 to arr2 and increase the size of arr2 dynamically while adding.

I tried adding a if condition

 if(!arr1[i].equals("a") && !arr1[i].equals("c")) {
     arr2 = Arrays.copyOf(arr1, i + incrementLength);
 }

but it still copies everything from arr1 to arr2.

import java.util.Arrays;
public class Test {
    public static void main(String[] args) {
        String[] arr1 = new String[] { "a", "b", "c", "d", "e" };
        String[] arr2 = new String[0];
        int incrementLength = 1;
        for (int i = 0; i < arr1.length; i++) {
            if(!arr1[i].equals("a") && !arr1[i].equals("c")) {
                arr2 = Arrays.copyOf(arr1, i + incrementLength);
            }
        }
        for (String value : arr2) {
            System.out.println(value);
        }
    }
}
like image 228
Test Account Avatar asked Oct 20 '25 12:10

Test Account


2 Answers

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:

    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:

    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:

    b
    d
    e

Hope this helps!

like image 165
anacron Avatar answered Oct 23 '25 02:10

anacron


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.

public 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.

like image 38
Eran Avatar answered Oct 23 '25 00:10

Eran