ArrayList Examples
ArrayList<Integer> guesses = new ArrayList<Integer>();
guesses.add(42);
guesses.add(13);
guesses.add(98);
int amountOfGuesses = guesses.size();
for (int counter = 0; counter < amountOfGuesses; counter++) {
System.out.println("Guess #" + counter + " is : " + guesses.get(counter));
}
ArrayList<String> students = new ArrayList<String>();
students.add("Mudkip");
students.add("Pikachu");
students.add("Gyrados");
int amountOfStudents = students.size();
for (int inc = 0; inc < amountOfStudents; inc++) {
System.out.println(students.get(inc));
}
Challenge
- Create an ArrayList of friends.
- Add all of your friends.
- Log the size of your friends.
- Then, the robot uprising happens! You survive! Your friends do not. Find out how to remove all of them at once from the ArrayList. Do it.
- Now, you've found some rocks, frogs, roaches, etc as friends.
- Create a new ArrayList of NewFriends
- Loop through all of your new friends
- And then watch the nuclear holocaust together.
- (This means use the method from step 4)
- Log the size of your list.
- BONUS: You survived. Wow. Create an ArrayList of reasonsToLive. Add a few.
- Change (or set) the first reason to something new.
- Change the last reason to something new.
- Log out the first and last reason to live.