// Hack 1
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
// a) ArrayList that stores Boolean values
ArrayList<Boolean> booleanList = new ArrayList<>();
// b) ArrayList that stores Turtle Objects
ArrayList<String> dogsList = new ArrayList<>();
// c) ArrayList that initializes with 10 Strings
ArrayList<String> stringList = new ArrayList<>(10);
stringList.add("one");
stringList.add("two");
stringList.add("three");
stringList.add("four");
stringList.add("five");
stringList.add("six");
stringList.add("seven");
stringList.add("eight");
stringList.add("nine");
stringList.add("ten");
booleanList.add(true);
// Print the contents of each ArrayList
System.out.println("Boolean List:");
for (Boolean b : booleanList) {
System.out.println(b);
}
System.out.println("Dogs List:");
for (String b : stringList) {
System.out.println(b);
}
System.out.println("String List:");
for (String s : stringList) {
System.out.println(s);
}
}
}
Example.main(null);
// Hack 2
import java.util.ArrayList;
public class Hack2 {
public static void main(Integer[] args) {
ArrayList<Integer> randomNumbers = new ArrayList<Integer>();
randomNumbers.add(1);
randomNumbers.add(4);
randomNumbers.add(7);
randomNumbers.add(12);
randomNumbers.add(23);
System.out.println("ArrayList: " + randomNumbers);
randomNumbers.remove(3); // Removes the element at index 3 (which is 12)
System.out.println("ArrayList: " + randomNumbers);
randomNumbers.set(2, 9); // Replaces the element at index 2 (which is 7) with 9
System.out.println("ArrayList: " + randomNumbers);
randomNumbers.add(6);
System.out.println("ArrayList: " + randomNumbers);
;
;
;
}
}
Hack2.main(null);