Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding integers to an int array

Tags:

java

arrays

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 illogical to me. I also tried addElement() and addInt(), however they don't work either.

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

}
like image 208
Rok Dolinar Avatar asked Mar 20 '15 17:03

Rok Dolinar


People also ask

How do you add integers to an array?

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.

How do you add values to an array in Java?

Hence this process involves the following steps. Convert Array into ArrayList using asList() method. Add elements into the array list using the add() method. Convert the ArrayList again to the array using the toArray() method.


5 Answers

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);
}
like image 125
Anderson Vieira Avatar answered Oct 19 '22 22:10

Anderson Vieira


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;
    }
}
like image 30
Eran Avatar answered Oct 19 '22 21:10

Eran


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.

like image 3
George Avatar answered Oct 19 '22 21:10

George


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++;
like image 2
rgettman Avatar answered Oct 19 '22 20:10

rgettman


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);
}
like image 3
Davis Broda Avatar answered Oct 19 '22 20:10

Davis Broda