在 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
Insert elements from one array to another array in Java
提问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 List
rather 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 list
where you will insert those elements then finally just convert this list
to an array
using .toArray
method:
您可以简单地使用list
将插入这些元素的位置,然后最终将其转换list
为array
using.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 array
of int
, you can :
如果您想使用array
of int
,您可以:
1-Use ArrayUtils
from 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 int
array
:
2-循环遍历列表元素并将它们放入int
array
:
int[] result = new int[list.size()];
int i = 0;
for (Integer e : list) {
result[i] = e.intValue();
i++;
}
return result;