ArrayList<E> Exercises

Completing this sequence of exercises will reacquaint you with the ArrayList<E> Collection and its implementation of some of the important List<E> methods including size, isEmpty, get, add, remove and compareTo.

Tasks.

  1. Create a project called ArrayListOps and add a driver by the same name.
  2. Recall the Really, Really Random Number Generator circuit from the Christmas holidays. About a third of the way down the page the author discusses John von Neumann's algorithm for evenly weighting a random set of binary elements (adjacent screen capture). Let's put his algorithm to the test,
    1. create and populate a 100-cell boolean array, named uneven, with random values
    2. determine and display the respective counts of uneven
    3. create an ArrayList<Boolean> named evenly, and, using von Neumann's algorithm, traverse uneven in the manner he describes and use the outcomes to populate evenly.
    4. determine and display the respective counts of evenly.
  3. Add the class field: ArrayList<String> names.
  4. In the main method, instantiate names and, immediately after, display the size() of the list.
  5. Use ArrayList's add(E) method to populate the list with a few names and then display the size of the list.
  6. Display the first object in the list.
  7. Display the last object in the list.
  8. Display the object in the list from back to front.
  9. Remove the first name in the list, display to verify, insert the name at the beginning again and display to verify. (4 statements)
  10. Make the first name the last name (1 statement). Display to verify.
  11. Remove the alphabetically-first name, reinsert it at the beginning of the list and display the list to verify.
  12. Implement a sort method that accepts names as the explicit parameter and returns a new list in alphabetic order. Display to verify.