简体   繁体   中英

Copy values from a larger size array to a smaller size array in Java

I have an array that holds these values: {0, 1, 3, 2, 3, 0, 3, 1}

I would like to call a static void remove() function that removes all the #3 values from the original array. I created a new array and with a smaller array size that will fit the numbers I need to be copied over. I loop through both arrays to copy the non #3 values over but keep getting the ArrayIndexOutOfBoundsException where the last destination index 6 out of bounds for int[5]

I believe this logic is correct, essentially, how do you copy values from a larger array to a small array? Am I missing another array to copy values over?

import java.util.Arrays;

public class Main {
    public static int[] remove(int v, int[] in) {
        int size = 0;
        for (int i=0; i<in.length; i++) {
            if (in[i] != v) {
                size++;
            }
        }
        int[] pushArray = new int[size];
        for (int j=0; j<pushArray.length; j++) {
            for (int i=0; i<in.length; i++) {
                if (in[i] == v) {
                    continue;
                } else {
                    System.arraycopy(in, in[i], pushArray, pushArray[i], pushArray.length);
                }
            }
        }
        return pushArray;
    }
    
    public static void main(String args[]) {
        System.out.println("Values from array: ");
        int[] intArray = new int[] {0, 1, 3, 2, 3, 0, 3, 1};
        System.out.println(Arrays.toString(intArray));
        
        int v = 3;
        
        System.out.println("Removing all values of " + v + " from array above ^");
        
        int[] newArray = remove(v, intArray);
        System.out.println(Arrays.toString(newArray));
    }
}

System.arraycopy doesn't copy an element from an array to another, it

Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.

means it copies consecutive elements from the source array, starting at srcPos, and puts them in the order they're in, in the destination array, starting from destPos.

what you're trying to do could be done by simply setting the value of pushArray[j] to in[i], without the need for the nested loop :

public static int[] remove(int v, int[] in) {
    int size = 0;
    for (int i = 0; i < in.length; i++) {
        if (in[i] != v) {
            size++;
        }
    }
    int[] pushArray = new int[size];

    int j = 0;
    for (int i = 0; i < in.length; i++) {
        if (in[i] != v) {
            pushArray[j++] = in[i];
        }
    }
    return pushArray;
}

Output :

Values from array: 
[0, 1, 3, 2, 3, 0, 3, 1]
Removing all values of 3 from array above ^
[0, 1, 2, 0, 1]

Try this.

public static int[] remove(int v, int[] in) {
    return IntStream.of(in)
        .filter(i -> i != v)
        .toArray();
}

public static void main(String[] args) {
    int[] in = {0, 1, 3, 2, 3, 0, 3, 1};
    int[] out = remove(3, in);
    System.out.println(Arrays.toString(out));
}

output:

[0, 1, 2, 0, 1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM