Java 将整数添加到 int 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29172164/
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 07:29:10  来源:igfitidea点击:

Adding integers to an int array

javaarrays

提问by Rok Dolinar

I am trying to add integers into an int array, but Eclipse says:

我正在尝试将整数添加到 int 数组中,但 Eclipse 说:

cannot invoke add(int) on the array type int[]

无法对数组类型 int[] 调用 add(int)

Which is completely illogical to me. I also tried addElement()and addInt(), however they don't work either.

这对我来说完全不合逻辑。我也试过 addElement()and addInt(),但是它们也不起作用。

public static void main(String[] args) {
    int[] num = new int[args.length];
    for (String s : args){
        int neki = Integer.parseInt(s);
        num.add(neki);

}

采纳答案by Anderson Vieira

To add an element to an array you need to use the format:

要将元素添加到数组,您需要使用以下格式:

array[index] = element;

Where arrayis the array you declared, indexis the position where the element will be stored, and elementis the item you want to store in the array.

哪里array是你声明的阵列,index就是元素将被存储的位置,并且element是要在阵列中存储的项目。

In your code, you'd want to do something like this:

在你的代码中,你想要做这样的事情:

int[] num = new int[args.length];
for (int i = 0; i < args.length; i++) {
    int neki = Integer.parseInt(args[i]);
    num[i] = neki;
}

The add()method is available for Collectionslike Listand Set. You could use it if you were using an ArrayList(see the documentation), for example:

add()方法可用于CollectionslikeListSet。如果您使用的是ArrayList(请参阅文档),则可以使用它,例如:

List<Integer> num = new ArrayList<>();
for (String s : args) {
    int neki = Integer.parseInt(s);
    num.add(neki);
}

回答by Eran

An array doesn't have an add method. You assign a value to an element of the array with num[i]=value;.

数组没有 add 方法。您可以使用 为数组的元素分配一个值num[i]=value;

public static void main(String[] args) {
    int[] num = new int[args.length];
    for (int i=0; i < num.length; i++){
      int neki = Integer.parseInt(args[i]);
      num[i]=neki;
    }
}

回答by rgettman

Arrays are different than ArrayLists, on which you could call add. You'll need an index first. Declare ibefore the forloop. Then you can use an array access expression to assign the element to the array.

数组与ArrayLists不同,您可以在其上调用add. 你首先需要一个索引。ifor循环之前声明。然后您可以使用数组访问表达式将元素分配给数组。

num[i] = s;
i++;

回答by Davis Broda

You cannot use the add method on an array in Java.

您不能在 Java 中对数组使用 add 方法。

To add things to the array do it like this

要将东西添加到数组中,请这样做

public static void main(String[] args) {
int[] num = new int[args.length];
for (int i = 0; i < args.length; i++){
    int neki = Integer.parseInt(s);
    num[i] = neki;

}

If you really want to use an add() method, then consider using an ArrayList<Integer>instead. This has several advantages - for instance it isn't restricted to a maximum size set upon creation. You can keep adding elements indefinitely. However it isn't quite as fast as an array, so if you really want performance stick with the array. Also it requires you to use Integer object instead of primitive int types, which can cause problems.

如果您真的想使用 add() 方法,请考虑使用 anArrayList<Integer>代替。这有几个优点 - 例如,它不受创建时设置的最大大小的限制。您可以无限期地继续添加元素。但是它没有数组那么快,所以如果你真的想要性能坚持数组。它还要求您使用 Integer 对象而不是原始 int 类型,这可能会导致问题。

ArrayList Example

ArrayList 示例

public static void main(String[] args) {
    ArrayList<Integer> num = new ArrayList<Integer>();
    for (String s : args){
        Integer neki = new Integer(Integer.parseInt(s));
        num.add(s);
}

回答by George

An array has a fixed length. You cannot 'add' to it. You define at the start how long it will be.

数组具有固定长度。你不能“添加”它。您在开始时定义它将持续多长时间。

int[] num = new int[5];

This creates an array of integers which has 5 'buckets'. Each bucket contains 1 integer. To begin with these will all be 0.

这将创建一个整数数组,其中包含 5 个“桶”。每个桶包含 1 个整数。首先,这些都将是0

num[0] = 1;
num[1] = 2;

The two lines above set the first and second values of the array to 1and 2. Now your array looks like this:

上面的两行将数组的第一个和第二个值设置为1and 2。现在你的数组看起来像这样:

[1,2,0,0,0]

As you can see you set values in it, you don't add them to the end.

正如您所看到的,您在其中设置了值,而不是将它们添加到末尾。

If you want to be able to create a list of numbers which you add to, you should use ArrayList.

如果您希望能够创建您添加的数字列表,您应该使用 ArrayList。

回答by Rafael Reis

you have an array of intwhich is a primitive type, primitive type doesn't have the method add. You should look for Collections.

你有int一个原始类型的数组,原始类型没有方法 add。你应该寻找Collections.

回答by Paul Cuddihy

org.apache.commons.lang.ArrayUtils can do this

org.apache.commons.lang.ArrayUtils 可以做到这一点

num = (int []) ArrayUtils.add(num, 12);     // builds new array with 12 appended