import java.util.ArrayList;
public class ArrayListExample {
private ArrayList<Integer> integerList = new ArrayList<>();
// Void method to add an integer to the ArrayList
public void addToArrayList(int value) {
integerList.add(value);
}
// Non-void method to get the value at a specified index
public int getValueAtIndex(int index) {
if (index >= 0 && index < integerList.size()) {
return integerList.get(index);
} else {
//
throw new IndexOutOfBoundsException("Yo this aint in there big dawg");
}
}
public static void main(String[] args) {
ArrayListExample example = new ArrayListExample();
// Adding values to the ArrayList
example.addToArrayList(10);
example.addToArrayList(20);
example.addToArrayList(30);
// Retrieving values from the ArrayList
int valueAtIndex1 = example.getValueAtIndex(1);
int valueAtIndex2 = example.getValueAtIndex(2);
System.out.println("Value at index 1: " + valueAtIndex1);
System.out.println("Value at index 2: " + valueAtIndex2);
}
}
ArrayListExample.main(null);
Value at index 1: 20
Value at index 2: 30