import java.util.List; /** * This class contains a number of recursive methods for lists. * * @author Franck van Breugel */ public class MyList { /** * Tests if the given list contains the given element. * * @param element the element. * @pre. element != null * @param list the list. * @pre. list != null * @return true if the given list contains the given element, * false otherwise. */ public static boolean contains(T element, List list) { boolean contains; if (list.size() == 0) { contains = false; } else { T first = list.remove(0); if (element.equals(first)) { contains = true; } else { contains = MyList.contains(element, list); } } return contains; } /** * Tests if the given list is constant, that is, if all * elements are the same. * * @param list the list. * @pre. list != null and list does not contain null * @return true if the given list is constant, false otherwise. */ public static boolean isConstant(List list) { boolean constant; if (list.size() <= 1) { constant = true; } else { if (!list.get(0).equals(list.get(1))) { constant = false; } else { list.remove(0); constant = MyList.isConstant(list); } } return constant; } /** * Returns the minimum element of the given list. * * @param list the list. * @pre. list != null and list.size() > 0 and list does not contain null * @return the minimum element of the given list. */ public static int min(List list) { int minimum; if (list.size() == 1) { minimum = list.get(0); } else { int first = list.remove(0); minimum = Math.min(first, MyList.min(list)); } return minimum; } /** * Tests if the given list consists of the powers of two, * that is, it is of the form [], [1, 2], [1, 2, 4] etc. * * @param list the list. * @return true if the given list consists of the powers of two, * false otherwise. */ public static boolean isPowersOfTwo(List list) { boolean valid; return valid; } }