Adding integers to an int array

前端 未结 7 849
我在风中等你
我在风中等你 2021-02-05 01:57

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

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

Which is completely

相关标签:
7条回答
  • 2021-02-05 02:14

    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.

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

    The two lines above set the first and second values of the array to 1 and 2. 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.

    0 讨论(0)
  • 2021-02-05 02:17

    An array doesn't have an add method. You assign a value to an element of the array with 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;
        }
    }
    
    0 讨论(0)
  • 2021-02-05 02:19

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

    num[i] = s;
    i++;
    
    0 讨论(0)
  • 2021-02-05 02:23

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

    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.

    ArrayList Example

    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);
    }
    
    0 讨论(0)
  • 2021-02-05 02:26

    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.

    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. You could use it if you were using an ArrayList (see the documentation), for example:

    List<Integer> num = new ArrayList<>();
    for (String s : args) {
        int neki = Integer.parseInt(s);
        num.add(neki);
    }
    
    0 讨论(0)
  • 2021-02-05 02:27

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

    0 讨论(0)
提交回复
热议问题