Source of ModificationSuccess.java


  1: import java.util.ArrayList;
  2: import java.util.List;
  3: import java.util.ListIterator;

  5: /**
  6:  * A program that correctly removes all negative numbers from a list.
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class ModificationSuccess {

 12:     public static void main(String[] args) {
 13:         List<Double> myNumbers = new ArrayList<Double>();

 15:         // Introduce
 16:         System.out.println("\n"
 17:                 + "Successful attempt to remove negative numbers from a list\n");

 19:         // create the list
 20:         myNumbers.add(20.5);
 21:         myNumbers.add(42.0);
 22:         myNumbers.add(40.2);
 23:         myNumbers.add(-5.3);
 24:         myNumbers.add(-8.0);
 25:         myNumbers.add(0.0);
 26:         myNumbers.add(15.1);
 27:         myNumbers.add(-30.7);
 28:         myNumbers.add(42.5);
 29:         System.out.println("Original list: " + myNumbers);

 31:         // remove all negative numbers
 32:         ListIterator<Double> it = myNumbers.listIterator();
 33:         while (it.hasNext()) {
 34:             double num = it.next();
 35:             if (num < 0.0) {
 36:                 it.remove();
 37:             }
 38:         }

 40:         // show the results
 41:         System.out.println("After removing negatives: " + myNumbers);
 42:         System.out.println("\nYAY!!!\n");
 43:     }

 45: }