ArrayList and Iterator: Example 3

This Java program demonstrates the use of the ArrayList class and the Iterator interface. The program allows the user to add or remove elements from a list of strings (in this case, names of cars).

The main class, ArrayListAddRemove, contains three methods:

  1. addOrRemoveElements: This method takes in 3 parameters – a character selection that represents the user’s choice to add or remove an element, a Scanner object reader for reading input from the user, and an ArrayList object list. This method continuously prompts the user to add or remove elements until the user decides to stop.
  2. printElements: This method takes in an ArrayList object list and outputs its elements using an iterator.
  3. main: This is the main method of the program. It initializes an ArrayList object cars with some default elements. It then prompts the user to choose whether they want to add or remove an element and calls the addOrRemoveElements method to perform the operation. Finally, it prints out the elements in the list after adding or removing elements.

The program makes use of the Scanner class for reading input from the user, the ArrayList class for creating and managing a dynamic list of elements, and the Iterator interface for iterating through the elements of the ArrayList.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

class ArrayListAddRemove {
	private static void addOrRemoveElements(char selection, Scanner reader, ArrayList list) {
		char c = 'y';
		String element;

		while (c == 'y') {
			switch (selection) {
			case 'a':
				System.out.print("Please enter element to add: ");
				element = reader.nextLine();
				list.add(element);

				System.out.print("continue adding? (y / n) ");
				c = reader.nextLine().charAt(0);
				break;
			case 'r':
				System.out.print("Please enter element to remove: ");
				element = reader.nextLine();

				if (list.contains(element)) {
					list.remove(element);
				} else {
					System.out.println("Element not found!");
				}

				System.out.print("continue removing? (y / n) ");
				c = reader.nextLine().charAt(0);
				break;
			default:
				System.out.print("Error");
				break;
			}
		}
	}

	private static void printElements(ArrayList list) {
		// output elements using iterator
		Iterator<String> iter = list.iterator();
		while (iter.hasNext()) {
			System.out.println(iter.next());
		}
	}

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		ArrayList<String> cars = new ArrayList<String>();

		cars.add("BMW");
		cars.add("Mercedes");
		cars.add("Ferrari");

		// print elements before adding or removing
		printElements(cars);

		System.out.print("Add or remove item? (a / r) ");
		char selection = reader.nextLine().charAt(0);

		addOrRemoveElements(selection, reader, cars);

		// print elements after adding or removing
		printElements(cars);
	}
}
Output
BMW
Mercedes
Ferrari
Add or remove item? (a / r) r
Please enter element to remove: BMW
continue removing? (y / n) y
Please enter element to remove: Bugatti
Element not found!
continue removing? (y / n) n
Mercedes
Ferrari