Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // RemoveAll
- // Spec: To remove the "all"
- // ArrayList remove() exercise
- import java.util.ArrayList;
- public class RemoveAll
- {
- public static void main(String args[])
- {
- ArrayList<String> ray;
- ray = new ArrayList<String>();
- int spot;
- ray.add("all"); // 1
- ray.add("all"); // 2
- ray.add("fun");
- ray.add("dog");
- ray.add("bat");
- ray.add("cat");
- ray.add("all"); // 3
- ray.add("dog");
- ray.add("all"); // 4
- ray.add("all"); // 5
- // the size of the arrayList ray
- // we initialize it here because you add everything first
- spot = ray.size() - 1;
- System.out.println(ray);
- System.out.println(ray.size());
- // add in a loop to remove all occurrences of all
- while (spot >= 0)
- {
- if (ray.get(spot).equalsIgnoreCase("all"))
- {
- ray.remove(spot);
- }
- spot = spot - 1;
- }
- System.out.println("\n" + ray);
- System.out.println(ray.size());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement