// Arup Guha
// 1/28/2017
// Example to show the basic use of an ArrayList in Java

import java.util.*;

public class testlist {

	public static void main(String[] args) {

		// Set up a single list and add 2 items.
		ArrayList<String> candy = new ArrayList<String>();
		candy.add("M+M's");
		candy.add("Snickers");
		System.out.println("candy list is now "+candy);

		// Set up a new list and add 1 item to it, see both lists.
		ArrayList<String> dessert = new ArrayList<String>();
		dessert.add("cheesecake");
		System.out.println("candy list is now "+candy);
		System.out.println("dessert list is now "+dessert);

		// Adding an item at a specified index.
		candy.add(1, "MilkyWay");
		System.out.println("at end candy is "+candy);
		candy.add("Twizzlers");
		candy.add("Sweettarts");
		candy.add("SwedishFish");
		candy.add("Skittles");

		// Get the user's favorite candy.
		Scanner stdin = new Scanner(System.in);
		System.out.println("What is your favorite candy?");
		String mine = stdin.next();

		// See if it's in our candy list!
		if (candy.contains(mine)) {

			System.out.println("We have your favorite candy!");

			// Here we will find the position of your favorite candy.
			int location = -1;
			
			// Try each possible location - make sure to use the equals method and not ==
			for (int i=0; i<candy.size(); i++) {
				if (mine.equals(candy.get(i))) {
					location = i;
					break;
				}
			}
			
			// Ta da!
			System.out.println("You can find your candy in location "+location);
		}
		
		// Didn't find it.
		else
			System.out.println("Sorry, try back later.");

		// Testing remove with an index.
		System.out.println(candy);
		candy.remove(3);
		System.out.println(candy);
		
		// And with an object.
		candy.add("M+M's");
		candy.add("M+M's");
		candy.add("M+M's");
		candy.add("Snickers");
		candy.add("M+M's");
		System.out.println(candy);
		candy.remove("M+M's");
		System.out.println(candy);
	}
}