简体   繁体   English

将整数添加到 int 数组

[英]Adding integers to an int array

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()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);

}

To add an element to an array you need to use the format:要将元素添加到数组,您需要使用以下格式:

array[index] = element;

Where array is the array you declared, index is the position where the element will be stored, and element is 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 Collections like List and Set .add()方法可用于CollectionsListSet You could use it if you were using an ArrayList (see the documentation ), for example:如果您使用的是ArrayList (请参阅文档),则可以使用它,例如:

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

An array doesn't have an add method.数组没有 add 方法。 You assign a value to an element of the array with num[i]=value;你用num[i]=value;为数组的元素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;
    }
}

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'.这将创建一个整数数组,其中包含 5 个“桶”。 Each bucket contains 1 integer.每个桶包含 1 个整数。 To begin with these will all be 0 .首先,这些都将是0

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

The two lines above set the first and second values of the array to 1 and 2 .上面的两行将数组的第一个和第二个值设置为12 Now your array looks like this:现在你的数组看起来像这样:

[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。

Arrays are different than ArrayList s, on which you could call add .数组与ArrayList不同,您可以在其上调用add You'll need an index first.你首先需要一个索引。 Declare i before the for loop.for循环之前声明i Then you can use an array access expression to assign the element to the array.然后您可以使用数组访问表达式将元素分配给数组。

num[i] = s;
i++;

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.如果您真的想使用 add() 方法,请考虑使用ArrayList<Integer>代替。 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.它还要求您使用 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);
}

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

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

you have an array of int which is a primitive type, primitive type doesn't have the method add.你有一个int数组,它是一个原始类型,原始类型没有 add 方法。 You should look for Collections .你应该寻找Collections

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

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