Problem 1: ArrayList Basics

Task 1: Insertion

Tacos Truck is now available down the street! New employees are struggling to get keep track of all the different orders. Help them organize all the items using ArrayLists!

/*
Dan is ordering from his favorite taco shop:
    - 2 orders of "carne asada"
    - 4 orders of "carnitas"
    - 1 order of "pollo"
    - 2 orders of "birria"

    Return an ArrayList of all of these elements in that order
*/

  1. How can you add items onto the list?
  2. What does Dan want in the order?

Task 2: Getting elements

A restaurant selling Chinese food has just opened nearby. A huge number of orders has just come in and the manager has trouble keeping track of everything, so they have put it all in an ArrayList. Help finish the program to return the given string at a given index.

ArrayList<String> menu = new ArrayList<>(); 

menu.add("Pizza"); 
menu.add("Hotdog"); 
menu.add("Hamburger"); 
menu.add("Hotdog"); 

// Returns "Pizza" since it is the 0th item in the menu
item = find(menu, 0);

  1. How can you iterate through the list?
  2. Look at the example menu!

Task 3: Removing Elements

The same Chinese restaurant has a bug in their code! Orders have been duplicated randomly and the ArrayList are filled with copies of orders. Help the owner out by writing a program to remove the first n occurrences of a given order in the ArrayList.

ArrayList<String> menu = new ArrayList<>(); 

menu.add("Pizza"); 
menu.add("Hotdog"); 
menu.add("Hamburger"); 
menu.add("Pizza");
menu.add("Pizza");
menu.add("Hotdog"); 

// Remove the first two Pizza orders
item = remove(menu,"Pizza", 2);

// Menu will not be the following array: {"Hotdog", "Hamburger", "Pizza", "Hotdog"}