import java.util.ArrayList; import java.util.Scanner; /** * Sorted Array List * Properly this should be generic and handle any data type. For today, just Strings * * @author gtowell * Created: Oct 8, 2020 * Modified: Oct 8, 2020 */ public class SAL { /** * The underlying arraylist. */ ArrayList sortedAL; /** * Default constructor. * Just initialize the array list */ public SAL() { sortedAL = new ArrayList<>(); } /** * Add a String to the sorted array list * @param stringToAdd */ public void add(String stringToAdd) { int loc = findPlace(stringToAdd); insertAtLoc(stringToAdd, loc); } /** * Find the place in the arraylist to put the string * @param toAdd the string to be added * @return the location in which to put the stirng */ private int findPlace(String toAdd) { int place=0; while (place=atLoc; ll--) { sortedAL.set(ll+1, sortedAL.get(ll)); } sortedAL.set(atLoc, toAdd); } /** * Override the default toString method, then use the toString of ArrayList */ public String toString() { return sortedAL.toString(); } public static void main(String[] args) { SAL sal = new SAL(); Scanner scann = new Scanner(System.in); System.out.print("Give me a String: "); String lin = scann.nextLine(); while (!lin.equals("0")) { sal.add(lin); System.out.print("Give me a String: "); lin = scann.nextLine(); } System.out.println("Q"); System.out.println(sal); } }