First of all, for initializing a container you cannot use a primitive type (i.e. int; you can use int[] but as you want just an array of integers, I see no use in that). Instead, you should use Integer, as follows:
ArrayList<Integer> arl = new ArrayList<Integer>();
For adding elements, just use the add function:
arl.add(1);
arl.add(22);
arl.add(-2);
Last, but not least, for printing the ArrayList you may use the build-in functionality of toString():
System.out.println("Arraylist contains: " + arl.toString());
If you want to access the i element, where i is an index from 0 to the length of the array-1, you can do a :
int i = 0; // Index 0 is of the first element
System.out.println("The first element is: " + arl.get(i));
I suggest reading first on Java Containers, before starting to work with them.
Answer from Raul Rene on Stack OverflowFirst of all, for initializing a container you cannot use a primitive type (i.e. int; you can use int[] but as you want just an array of integers, I see no use in that). Instead, you should use Integer, as follows:
ArrayList<Integer> arl = new ArrayList<Integer>();
For adding elements, just use the add function:
arl.add(1);
arl.add(22);
arl.add(-2);
Last, but not least, for printing the ArrayList you may use the build-in functionality of toString():
System.out.println("Arraylist contains: " + arl.toString());
If you want to access the i element, where i is an index from 0 to the length of the array-1, you can do a :
int i = 0; // Index 0 is of the first element
System.out.println("The first element is: " + arl.get(i));
I suggest reading first on Java Containers, before starting to work with them.
More simple than that.
List<Integer> arrayIntegers = new ArrayList<>(Arrays.asList(1,2,3));
arrayIntegers.get(1);
In the first line you create the object and in the constructor you pass an array parameter to List.
In the second line you have all the methods of the List class: .get (...)
arrays - Java ArrayList for integers - Stack Overflow
What's the difference between new ArrayList<Integer>() and new ArrayList();
How do I make integer lists in Java?
How to create ArrayList (ArrayList<Integer>) from array (int[]) in Java - Stack Overflow
List of Integer.
List<Integer> list = new ArrayList<>();
int x = 5;
list.add(x);
You are trying to add an integer into an ArrayList that takes an array of integers Integer[]. It should be
ArrayList<Integer> list = new ArrayList<>();
or better
List<Integer> list = new ArrayList<>();
The difference between the datatype and the constructor was confusing to me, so I did some tests.
ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(0);
in this example "arr.get(0)", will give us an Integer (as intended)
ArrayList arr = new ArrayList<Integer>(); arr.add(0);
In this one it will give us an Object so we need to cast it.
ArrayList<Integer> arr = new ArrayList(); arr.add(0);
In this one it will also give us an Integer (*like the first one*)
So my question is, what's the difference between the first and last one? why would I write ArrayList<Integer>()? if only ArrayList(); will do the work?
It seems a lot easier from the very little time I spent on Python before making the jump to Java. I'm taking a college night class on Java, and lists are one thing that are confusing me. How do they work in Java?
The problem in
intList = new ArrayList<Integer>(Arrays.asList(intArray));
is that int[] is considered as a single Object instance since a primitive array extends from Object. This would work if you have Integer[] instead of int[] since now you're sending an array of Object.
Integer[] intArray = new Integer[] { 0, 1 };
//now you're sending a Object array
intList = new ArrayList<Integer>(Arrays.asList(intArray));
From your comment: if you want to still use an int[] (or another primitive type array) as main data, then you need to create an additional array with the wrapper class. For this example:
int[] intArray = new int[] { 0, 1 };
Integer[] integerArray = new Integer[intArray.length];
int i = 0;
for(int intValue : intArray) {
integerArray[i++] = intValue;
}
intList = new ArrayList<Integer>(Arrays.asList(integerArray));
But since you're already using a for loop, I wouldn't mind using a temp wrapper class array, just add your items directly into the list:
int[] intArray = new int[] { 0, 1 };
intList = new ArrayList<Integer>();
for(int intValue : intArray) {
intList.add(intValue);
}
With Java 8 you can do it in one line:
int[] intArr = { 1, 1, 2, 3, 5, 8, 11};
List<Integer> list = Arrays.stream(intArr).boxed().collect(Collectors.toList());
list.forEach(System.out::println);