简体   繁体   English

在 Java 中将值从较大的数组复制到较小的数组

[英]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}我有一个包含这些值的数组: {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.我想调用一个静态 void remove() 函数,该函数从原始数组中删除所有 #3 值。 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]我循环遍历两个数组以复制非 #3 值,但不断获取ArrayIndexOutOfBoundsException ,其中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 System.arraycopy不会将一个元素从一个数组复制到另一个,它

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.从指定的源数组复制一个数组,从指定位置开始,到目标数组的指定位置。数组组件的子序列从 src 引用的源数组复制到 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.源数组中位置 srcPos 到 srcPos+length-1 处的组件分别复制到目标数组的位置 destPos 到 destPos+length-1。

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.意味着它从源数组中复制连续的元素,从 srcPos 开始,并将它们按照它们所在的顺序放入目标数组中,从 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 :您可以通过简单地将 pushArray[j] 的值设置为 in[i] 来完成您想要做的事情,而无需嵌套循环:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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