How to add Integer Values to ArrayList, int array Examples

1. Introduction


In this tutorial, We'll learn an ArrayList problem of how to add integer values to an ArrayList. Let us write an example program to add primitive int values and wrapper integer objects.


In the previous articles, we have discussed how to remove duplicate values in ArrayList.

In the next article, We'll learn How to Iterate ArrayList values?

How to add Integer Values to ArrayList, int array Examples



2. Adding primitive int values to ArrayList


Let us write a sample program to add primitive int values to List. Take a look at the below program that List object is created as new ArrayList<int> and assigned the reference to List<int>.

package com.java.w3schools.blog.arraylist;

import java.util.ArrayList;
import java.util.List;

/**
*
* Adding primitive int to ArrayList
*
* @author javaprogramto.com
*
*/
public class ArrayListAddPrimitiveIntGenerics {

public static void main(String[] args) {

List l = new ArrayList();
l.add(100);
l.add(200);
l.add(309);

}

}

Output:

When compile this program generates a compile-time error.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Syntax error, insert "Dimensions" to complete ReferenceType
Syntax error, insert "Dimensions" to complete ReferenceType

at com.java.w3schools.blog.arraylist.ArrayListAddPrimitiveIntGenerics.main(ArrayListAddPrimitiveIntGenerics.java:17)

Still, there is a way to avoid this error without specifying the int type as List instead of List<int>

import java.util.ArrayList;
import java.util.List;

/**
*
* Adding primitive int to ArrayList
*
* @author javaprogramto.com
*
*/
public class ArrayListAddPrimitiveInt {

public static void main(String[] args) {

List l = new ArrayList();
l.add(10);
l.add(20);
l.add(30);
l.add(40);
l.add(50);
l.add(60);

System.out.println("primitive list values : " + l);

}

}


ArrayList object l in the program will accept any type of values but here we are adding only primitive type int values.

Output:

primitive list values : [10, 20, 30, 40, 50, 60]

3. Adding Integer Wrapper Objects to List


In this program, an example is shown with generics by specifying the type of values that Arraylist accepts. List<Integer> indicates that it takes only Integer objects.
But, in the code passed only primitives. Let us compile the code and see the output.

public class ArrayListAddPrimitiveIntegers {

public static void main(String[] args) {

List l = new ArrayList<>();
l.add(1);
l.add(2);
l.add(3);
l.add(4);
l.add(5);

System.out.println("wrapper integers list values : " + l);

}

}

Output:

This program compiles and runs without any issues.

wrapper integers list values : [1, 2, 3, 4, 5]

Because java JVM does the autoboxing that converts primitive to boxed object.

4. ArrayList of int array in java


public class ArrayListAddPrimitiveIntegersArray {

public static void main(String[] args) {

List list = new ArrayList<>();

// int[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Integer[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

list.add(0, intArray);

System.out.println("int array to arraylsit : " + list);

}

}

Output:

int array to arraylsit : [[Ljava.lang.Integer;@4e25154f]

To see the values inside the int[] array, we need to iterate over int[] array.

ArrayList of int array in java


Printing the values inside int[] array of ArrayList (ArrayList<Integer[]>)


for (int i = 0; i < list.size(); i++) {
Integer[] array = list.get(i);

for (int j : array) {
System.out.println(j);
}

}

Output:

1
2
3
4
5
6
7
8
9

5. Conclusion


In this short article, we have seen how to add int values to ArrayList and also adding int array to List.

Reference

0 Comments