13. Practice A

Two practice problems covering the basic properties (and gotcas) of ArrayList.

ArrayList and Random

  1. Make an ArrayList of Integer.
  2. Add in a random number (10 through 20) of random integers in the range [-50,50].
  3. Change 3 random locations in your arraylist to -500.
  4. Remove the items at index 2 and 6 and print out their sum.
  5. Print out the number of items in the ArrayList.
  6. Modify the list by removing all of the negative numbers.
  7. Print the list.

Null

String bad = null;
String empty = "";
  1. Input an array of integers.

       public static void prob2(int[] xs)
    
  2. Output an ArrayList of the same length.

    Translate each of the numbers into a string:

      0 -> null
      1 -> "One"
      3 -> "Three"
      _ -> "."
    
  3. Print the total # characters in all of the Strings in the ArrayList.

  4. Now remove all of the “Three” strings in the ArrayList.

  5. Print the remaining ArrayList.