The implementation ArrayListuses Arrayunder the hood. However, Arraysinitialized to default values (0 or null), but ArrayListjust empty. Why is this?
int[] arr = new int[10];
String[] arr1 = new String[11];
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr1));
List<Integer> list = new ArrayList<Integer>(10);
System.out.println(list);
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[null, null, null, null, null, null, null, null, null, null, null]
[]
This means that every time I use ArrayList, I need to fill in the material; I tried the next part in my code and it threw NoSuchElementException, and then I realized that this is not the default, where Arraysdo
if (list.get(i)==null){
list.add(i,x);
else:
list.add(i,list.get(i)+x)
EDIT:
even List<Integer> list = new ArrayList<Integer>(10);
prints [] although I initialized the size;
source
share