/* * ListClient.java * * CS 112 - Lab 9 */ public class ListClient { public static void main(String[] args) { // changing from ArrayList to LLList does not change the results! List vals1 = new LLList(); // List vals1 = new ArrayList(10); // Do **NOT** change these lines! vals1.addItem("hello", 0); vals1.addItem("world!", 0); vals1.addItem("how", 1); vals1.addItem("are", 1); vals1.addItem("you?", 2); System.out.println(vals1); // Task 1: Add lines below to reorder the items in vals1 as instructed, // and then print the new contents of vals1. // Move "hello" from position 4 to position 0. String item = (String)vals1.removeItem(4); vals1.addItem(item, 0); // "how" is now in position 4. // Move it from there to position 2. item = (String)vals1.removeItem(4); vals1.addItem(item, 2); System.out.println(vals1); // test calls for contains if (contains(vals1, "hello")) { System.out.println("found hello"); } else { System.out.println("did not find hello"); } if (contains(vals1, "goodbye")) { System.out.println("found goodbye"); } else { System.out.println("did not find goodbye"); } } /** First method for Task 2 **/ public static boolean contains(List items, Object item) { if (items == null || item == null) { return false; } for (int i = 0; i < items.length(); i++) { Object itemAt = items.getItem(i); // get item at position i if (itemAt.equals(item)) { return true; } } return false; } /** Second method for Task 2 **/ public static boolean contains2(List items, Object item) { if (items == null || item == null) { return false; } ListIterator iter = items.iterator(); while (iter.hasNext()) { Object itemAt = iter.next(); if (itemAt.equals(item)) { return true; } } return false; } }