ArrayList Practice

posted by: Ms. Martin 22 February 2010 No Comment

Check out the ArrayList slides to make sure you understand how to use all of the methods and to make sure you understand the generic syntax.

We’ll just spend today and a little of Wednesday doing some mechanical exercises to get you used to the syntax and then dive into a larger project.

Negative Integers

Write a method that prints all negative values in an ArrayList of Integers.  Use an enhanced for loop.

Max Length

Write a method maxLength that takes an ArrayList of Strings as a parameter and that returns the length of the longest string in the list. If your method is passed an empty list, it should return 0.  Use the enhanced for loop syntax.

Remove even-indexed

Write a method that changes every even-indexed element of an ArrayList of strings to the empty string.

Swap

Write a method that takes an ArrayList of Strings and two integer indexes as parameters. Your method should swap the values at the two indexes.

Grocery List

Write a program that will simulate creating and crossing off a grocery list. You should print out the current grocery list then provide the user with the following options:
  1. Add an item to the list.
  2. Remove an item on the list (if it is on the list – otherwise inform them that the item was not on the list).
  3. Replace an item on the list. Display the list and the total number of items in the list.
  4. Quit
The program should not end until the user decides to quit.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>