在 Java 中将元素从一个数组插入到另一个数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30849660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:14:49  来源:igfitidea点击:

Insert elements from one array to another array in Java

javaarrays

提问by Kaveen

I'm trying to identify multiples of 3 from an input array and insert those multiples in to output array.Using a count variable I can declare the output array. But then how can I insert multiples in to output array?.

我试图从输入数组中识别 3 的倍数并将这些倍数插入到输出数组中。使用计数变量我可以声明输出数组。但是,如何将倍数插入到输出数组中?

public class MultipleOfThreeInAnArray {

static int count;
static int[] result;


public static void choseMultiplesOfThree(int[] input) {

    for (int i = 0; i < input.length; i++) {

        if (input[i] % 3 == 0) {
            count++;

            // insert into output array?
            int[] result = new int[count];

        }
    }
public static void main(String[] args) {
       int[] input = { 3, 2, 5, 6, 9, 30 };
       choseMultiplesOfThree(input);

    }
}

采纳答案by Uma Kanth

public class MultipleOfThreeInAnArray {
    public static void choseMultiplesOfThree(int[] input) {
        int[] output = new int[input.length];
        int index = 0;
        for (int i = 0; i < input.length; i++) {
            if (input[i] % 3 == 0) {
                output[index++] = input[i];
            }
        }
        System.out.println(Arrays.toString(output));
    }

    public static void main(String[] args) {
        int[] input = {3, 2, 5, 6, 9, 30};
        choseMultiplesOfThree(input);
    }
}

I suggest you to use a Listrather than an array.

我建议您使用 aList而不是数组。

public class MultipleOfThreeInAnArray {
    public static void choseMultiplesOfThree(int[] input) {
        List<Integer> output = new ArrayList<Integer>();
        for (int i = 0; i < input.length; i++) {
            if (input[i] % 3 == 0) {
                output.add(input[i]);
            }
        }
        System.out.println(output);
    }

    public static void main(String[] args) {
        int[] input = {3, 2, 5, 6, 9, 30};
        choseMultiplesOfThree(input);
    }
}

回答by cн?dk

You can Simply use a listwhere you will insert those elements then finally just convert this listto an arrayusing .toArraymethod:

您可以简单地使用list将插入这些元素的位置,然后最终将其转换listarrayusing.toArray方法:

public class MultipleOfThreeInAnArray {
    static Integer[] result;
    List<Integer> list = new ArrayList<Integer>();


    public static void choseMultiplesOfThree(int[] input) {
        for (int i = 0; i < input.length; i++) {
            if (input[i] % 3 == 0) {
                // insert into output array
                list.add(Integer.valueOf(input[i]));
            }
        }
    result = list.toArray(new Integer[list.size()]);
    }

    public static void main(String[] args) {
           Integer[] input = { 3, 2, 5, 6, 9, 30 };
           choseMultiplesOfThree(input);
        }
    }

That should do the trick, and note that you have to use Integer with List.

这应该可以解决问题,并注意您必须将 Integer 与List.

EDIT:

编辑:

If you want to use an arrayof int, you can :

如果您想使用arrayof int,您可以:

1-Use ArrayUtilsfrom apache commons:

1-ArrayUtils来自apache commons 的使用:

int[] result = ArrayUtils.toPrimitive(list.toArray(new int[list.size()]));

2-Loop throught the list elements and put them in an intarray:

2-循环遍历列表元素并将它们放入intarray

    int[] result = new int[list.size()];
    int i = 0;
    for (Integer e : list) {
        result[i] = e.intValue();
        i++;
    }
    return result;