Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using add(). Elements not yet added do not exist until explicitly inserted.
Passing as argument - a copy of the reference to the instantiated ArrayList is passed to the method. This means that any changes made to the elements inside the method persist outside the method. The exception is if the argument is assigned to reference a different ArrayList inside the method.
Q20: Put the following code in order to create a program that will declare and instantiate an ArrayList of 10 random values between 1 and 100 and then find the maximum value in the ArrayList.
import java.util.*;
public class main{
public static void main (String[] args) {
---
Random r = new Random();
ArrayList<Integer> list = new ArrayList<>();
---
for (int i = 0; i < 10; i++) {
list.add(r.nextInt(100) + 1);
System.out.println("list["+i+"] is " + list.get(i));
}
---
int max = list.get(0);
---
for (int i = 1; i < 10; i++) {
---
if (list.get(i) > max)
---
max = list.get(i);
---
}
---
System.out.println("Maximum value is " + max);
---
}
}