Advertisement
Crenox

RemoveAll ArrayList Java Program

Dec 23rd, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. // Sammy Samkough
  2. // RemoveAll
  3. // Spec: To remove the "all"
  4. // ArrayList remove() exercise
  5.  
  6. import java.util.ArrayList;
  7.  
  8. public class RemoveAll
  9. {
  10. public static void main(String args[])
  11. {
  12. ArrayList<String> ray;
  13. ray = new ArrayList<String>();
  14. int spot;
  15.  
  16. ray.add("all"); // 1
  17. ray.add("all"); // 2
  18. ray.add("fun");
  19. ray.add("dog");
  20. ray.add("bat");
  21. ray.add("cat");
  22. ray.add("all"); // 3
  23. ray.add("dog");
  24. ray.add("all"); // 4
  25. ray.add("all"); // 5
  26.  
  27. // the size of the arrayList ray
  28. // we initialize it here because you add everything first
  29. spot = ray.size() - 1;
  30.  
  31. System.out.println(ray);
  32. System.out.println(ray.size());
  33.  
  34. // add in a loop to remove all occurrences of all
  35. while (spot >= 0)
  36. {
  37. if (ray.get(spot).equalsIgnoreCase("all"))
  38. {
  39. ray.remove(spot);
  40. }
  41. spot = spot - 1;
  42. }
  43.  
  44. System.out.println("\n" + ray);
  45. System.out.println(ray.size());
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement