/** * Simple class for storing a list of Strings. * Demonstration program for CSE 143. * Starter code for program 1. * @author Hal Perkins * @version 0.2, 3/30/06 */ public class StringList { // Object state private String[] items; // List elements are stored in private int size; // items[0..size-1] // Default size of new lists private static final int DEFAULT_CAPACITY = 20; // constructors /** * Construct a new, empty StringList with a default capacity */ public StringList() { // Let constructor with explicit capacity do the work this(DEFAULT_CAPACITY); } /** * Construct a new, empty Stringlist with a specified capacity * @param capacity maximum number of items this list can hold */ public StringList(int capacity) { if (capacity < 1) { throw new IllegalArgumentException(); } items = new String[capacity]; size = 0; } // private methods used by other operations // Verify that pos is valid (0 <= pos < size) // Throw an IndexOutOfBoundsException if it is not, otherwise // return silently. private void checkIndex(int pos) { if (pos < 0 || pos >= size) { throw new IndexOutOfBoundsException(); } } // query functions /** * Return the size of this list * @return number of items currently in the list */ public int size() { return size; } /** * Report whether this list is empty or not * @return true if the list is empty, otherwise false */ public boolean isEmpty() { return size() == 0; } /** * Return the location of a string in this list * @param s the string to be located * @return the index of s in the list if it is present, otherwise -1 */ public int indexOf(String s) { for (int k = 0; k < size; k++) { if (items[k].equals(s)) { return k; } } // not found return -1; } /** * Report whether this list contains a particular string * @param s string to search for * @return true if s is contained in the list, false otherwise */ public boolean contains(String s) { return indexOf(s) >= 0; } /** * Return the list element at a given position in this list * @param pos position of desired element (must be 0 <= pos < size) * @return the list element at that position */ public String get(int pos) { checkIndex(pos); return items[pos]; } // string modifications /** * Change the item at a given position to a new value * @param pos position in the list to be changed (must be 0 <= pos < size) * @param s new value to put at that position */ public void set(int pos, String s) { checkIndex(pos); items[pos] = s; } /** * Add a new item to the end of this StringList. * pre: list is not full * @param s the string to add to the list */ public void add(String s) { if (size == items.length) { throw new IndexOutOfBoundsException(); } items[size] = s; size++; } /** * Add a new item to this list at a specified position * @param pos location where string should be added * (pre: 0 <= pos < size, and list is not full) * @parm s string to be added */ public void add(int pos, String s) { checkIndex(pos); if (size == items.length) { throw new IndexOutOfBoundsException(); // no room } for (int k = size; k > pos; k--) { items[k] = items[k-1]; } items[pos] = s; size++; } /** * Add the contents of another StringList to the end of this list. * pre: there is enough room in this list for the contents of the other list * @param other the other StringList */ public void add(StringList other) { if (this.size + other.size() > this.items.length) { throw new IndexOutOfBoundsException(); } for (int k = 0; k < other.size(); k++) { this.add(other.get(k)); } } /** * Remove the item at the specified position from this list * @param pos position of the item to be removed (must be 0 <= pos < size) */ public void remove(int pos) { checkIndex(pos); for (int k = pos; k < size-1; k++) { items[k] = items[k+1]; } size--; } // toString /** * Return a string representation of this StringList * @return A list of the items in this list separated by commas * and surrounded by brackets */ public String toString() { String result = "["; if (size > 0) { result = result + items[0]; } for (int k = 1; k < size; k++) { result = result + "," + items[k]; } result = result + "]"; return result; } }