//Class to allow storing a collection of String items //Supports the following operations // size : obtain a count of the stored items // has : check is an item is stored in the list // add : store a new item // del : remove a stored item // import java.util.ArrayList; import java.util.Arrays; public class StringList { private String title; private ArrayList list; //Constructor: creates an empty StringList public StringList(String title) { this.title = title; this.list = new ArrayList(); } public StringList() { this(""); } public String toString() { String image = this.title + " " + this.list.size() + " Entries"; /* for (int k = 0; k < list.size(); k++) image += "\n" + list.get(k); */ for (String item : this.list) image += "\n" + item; return image; } //Accessor: returns the number of items in the list public int size() { return this.list.size(); } //Accessor: returns true iff the list contains a specified item public boolean has(String item) { return this.list.contains(item); } //Mutator: Adds a new item to the list // List unchanged if the item is already stored public void add(String item) { if (!this.list.contains(item)) this.list.add(item); } //Mutator: Removes an item from the list // List unchanged if the item is not stored public void del(String item) { this.list.remove(item); } //Mutator: Sorts the list into Ascending order public void sort() { ArrayList sorted = new ArrayList(); while ( !this.list.isEmpty() ) { String item = smallest(); list.remove( item ); sorted.add( item ); } this.list = sorted; } private String smallest() { String small = list.get(0); for (String next : list) if ( next.compareTo(small) < 0 ) small = next; return small; } /* **** ARRAY implementation **** private static final int DEFAULT_SIZE = 4; private String title; private int size; private String[] list; //Constructor: creates an empty StringList public StringList(String title) { this.title = title; this.list = new String[DEFAULT_SIZE]; this.size = 0; } public StringList() { this(""); } public String toString() { String image = this.title + " " + this.size + " Entries"; for (int k = 0; k < size; k++) image += "\n" + this.list[k]; return image; } //Accessor: returns the number of items in the list public int size() { return this.size; } //Accessor: returns true iff the list contains a specified item public boolean has(String item) { return this.indexOf(item) != -1; } //Mutator: Adds a new item to the list // List unchanged if the item is already stored public void add(String item) { if (this.indexOf(item) != -1) return; if (this.size == this.list.length) extendList(); this.list[size] = item; this.size++; } //Mutator: Removes an item from the list // List unchanged if the item is not stored public void del(String item) { int index = this.indexOf(item); if (index == -1) return; for (int i = index+1; i < this.size; i++) this.list[i - 1] = this.list[i]; this.size--; } //Helper: Returns the index of the specified item in the array // Returns -1 if the item is not stored private int indexOf(String item) { int index = this.size - 1; while (index >= 0 && !item.equalsIgnoreCase(this.list[index])) index--; return index; } //Helper: Creates additional storage in the list private void extendList() { String[] store = new String[this.list.length + DEFAULT_SIZE]; for (int i = 0; i < this.list.length; i++) store[i] = this.list[i]; this.list = store; } */ }