// From A Beginning Programmer's Guide to Java
// at http://beginwithjava.blogspot.com/

// Referenced in the articles 'The List in Java'
// at http://beginwithjava.blogspot.com/2010/04/list-in-java.html
// and 'Lists in Java: Sample Code'
// at http://beginwithjava.blogspot.com/2010/04/lists-in-java-sample-code.html

/* Playing around with Lists, in basic ways.
  We use the ArrayList class as an implementation of List,
  since at this point we're just using a List's features,
  rather than learning about building our own List-based
  class.

  -Mark Graybill, Apr. 2010
*/

import java.util.*; // include the package with List in it.

public class Lister{
  public static void main(String arg[]){

  // Create some Lists to play with.
    ArrayList lister = new ArrayList();
    ArrayList dave = new ArrayList();
    ArrayList kryten = new ArrayList();

  // Put some things in the lister List, manually.
    lister.add("The End");
    lister.add("Future Echoes");
    lister.add("Confidence and Paranoia");
    lister.add("Thanks for the Memories");

  // Print the current lister list.
    System.out.print(lister);
    System.out.println();

  /* Get a sublist from lister.
    This will get items at indices 1 and 2. Stops
    short of item 3. I.e., gets sublist from element
    1 up to, but not including, 3. */
    dave.addAll(lister.subList(1,3));

    System.out.print("Sublist elements 1 and 2: ");
    System.out.print(dave);
    System.out.println();

  // Put something in the kryten list.
    kryten.add("The Rimmer Experience");
    kryten.add("Ace");

  // See if kryten is in the lister list.
    if (lister.containsAll(kryten)){
      System.out.println("All of kryten is in lister.");
    }
    else {
      System.out.println("Items in kryten aren't in lister.");
    }

  // Do the same thing with the dave list.
    if (lister.containsAll(dave)){
      System.out.println("Items in dave are all in lister.");
    }
    else {
      System.out.println("Items in dave aren't in lister.");
    }

  // Step through the list with a for-each
    System.out.println(); // Get a blank line.
    System.out.println("Items in Lister:"); // Title the list.
    for (Object epname: lister){ // Print the list.
      System.out.println(epname);
    }

  /* Note: the above code is a little bit "dirty", in that we're counting on
    the objects we get from lister to be printable. Since we've had tight
    control over what goes in, we can get away with this. If you make a List
    that lets anything in, make sure they've all got toString() methods, or
    otherwise take care of object types. Generics are a partial solution to
    this problem (though they're less than perfect.)
  */

  // Get an iterator and use it.
    ListIterator iter = lister.listIterator();

  // Iterator starts out with the first element in List as its "next()"
    System.out.println();
    System.out.println(iter.next());

  // Doing next() has advaned our iterator in the list,
  // so if we do it again:
    System.out.println(iter.next());
  // We see we have advanced. And we advance again.

  // We can it to go through the list, use hasNext() to watch for the end.
    System.out.println("\nGoing forward:");
    while (iter.hasNext()){
      System.out.println(iter.next());
  }

  // And we can go backward:
    System.out.println("\nGoing backward:");
    while (iter.hasPrevious()){
      System.out.println(iter.previous());
    }

  // Print the element in front of "Thanks for the Memories".
    System.out.println("\nItem before \"Thanks for the Memories\" is:");
    while (iter.hasNext()){
      if ( iter.next().toString().contentEquals("Thanks for the Memories") ){
        iter.previous(); //we found it, now move back to it.
        System.out.println(iter.previous());       // Two steps to get back.
        break; // We've done it, now get out of the loop.
      }
    }

  } // end of main()
} // end of Lister