// // P.855 Use ArrayList for integer list operations // import java.util.*; public class ArrayListDemo { public static void main(String[] args) { Scanner kb = new Scanner(System.in); boolean go = true; String answer; int num; ArrayList arrayList = new ArrayList(); Comparator comparator = Collections.reverseOrder(); while(go) { menu(); answer = kb.next(); if(answer.equalsIgnoreCase("I")) { System.out.print("\nInput an integer: "); num = kb.nextInt(); arrayList.add(num); } else if(answer.equalsIgnoreCase("S")) { // sort System.out.println("Will do sorting - ascending"); Collections.sort(arrayList); System.out.println("Done the sort."); } else if(answer.equalsIgnoreCase("D")) { // sort to descending System.out.println("Will do sorting - descending"); Collections.sort(arrayList, comparator); System.out.println("Done the sort."); } else if(answer.equalsIgnoreCase("F")) { // search System.out.println("Will do searching"); System.out.print("Input the item to be found: "); int item = kb.nextInt(); boolean isOn = arrayList.contains(item); if(isOn) System.out.println(item + " is on.\n"); else System.out.println(item + " is NOT on.\n"); } else if(answer.equalsIgnoreCase("R")) { // remove System.out.println("Will do remove"); System.out.print("Input the item to be removed: "); int item = kb.nextInt(); boolean isDone = arrayList.remove((Integer)item); if(isDone) System.out.println(item + " is succefully removed.\n"); else System.out.println(item + " is NOT on the list. Failed\n"); } else if(answer.equalsIgnoreCase("Z")) { int size = arrayList.size(); System.out.print("\nThe size of the list is : " + size); } else if(answer.equalsIgnoreCase("P")) { System.out.print("\nThe List is: " + arrayList); System.out.println("\n"); } else if(answer.equalsIgnoreCase("Q")) go = false; } } public static void menu() { System.out.println("\n\n-----------------\nI Insert an item."); System.out.println("S Sort the list to ascending order."); System.out.println("D Sort the list to descending order."); System.out.println("F Find if an item in on the list."); System.out.println("P Print the list."); System.out.println("R Remove an item."); System.out.println("Z Display the size of the list."); System.out.println("Q Quit."); System.out.println("------------------------------------------\n\n"); } }