Answer:
Answer code is given below along with comments to explain each step
Explanation:
Method 1:
// size of the array numbers to store 1 to 5 integers
int Size = 5;
// here we have initialized our array numbers, the type of array is integers and size is 5, right now it is empty.
ArrayList<Integer> numbers = new ArrayList<Integer>(Size);
// lets store values into the array numbers one by one
numbers.add(1)
numbers.add(2)
numbers.add(3)
numbers.add(4)
numbers.add(5)
// here we are finding the first element in the array numbers by get() function, the first element is at the 0th position
int firstElement = numbers.get(0);
// to print the first element in the array numbers
System.out.println(firstElement);
Method 2:
int Size = 5;
ArrayList<Integer> numbers = new ArrayList<Integer>(Size);
// here we are using a for loop instead of adding manually one by one to store the values from 1 to 5
for (int i = 0; i < Size; i++)
{
// adding the values 1 to 5 into array numbers
numbers.add(i);
}
//here we are finding the first element in the array numbers
int firstElement = numbers.get(0);
// to print the first element in the array
System.out.println(firstElement);