Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ListOfStrings {
- public static final int DEFAULT_CAPACITY = 4;
- private String[] names;
- private int size = 0;
- public ListOfStrings() {
- this(DEFAULT_CAPACITY);
- }
- public ListOfStrings(int initialCapacity) {
- names = new String[initialCapacity];
- }
- public int getCapacity() {
- return names.length;
- }
- public int getSize() {
- return size;
- }
- // Adds name to the end of the list
- public void add(String name) {
- if (this.getSize() == this.getCapacity()) {
- // Double the size of the array
- String[] temp = new String[this.getCapacity() * 2];
- System.arraycopy(names, 0, temp, 0, this.getCapacity());
- names = temp;
- }
- names[size] = name;
- size++;
- }
- // set item i to the given name
- public void set(int i, String name) {
- if (i < 0 || i > this.getSize() - 1) {
- // we have a problem
- String message = "index " + i + " not valid";
- throw new IndexOutOfBoundsException(message);
- }
- names[i] = name;
- }
- // returns the item at index i
- public String get(int i) {
- if (i < 0 || i > this.getSize() - 1) {
- // we have a problem
- String message = "index " + i + " not valid";
- throw new IndexOutOfBoundsException(message);
- }
- return names[i];
- }
- // removes and returns item i from the list
- public String remove(int i) {//
- if (i < 0 || i > this.getSize() - 1) {
- // we have a problem
- String message = "index " + i + " not valid";
- throw new IndexOutOfBoundsException(message);
- }
- String removedItem = this.get(i); // save item to return
- // now adjust the array
- if (i < size - 1) {
- System.arraycopy(names, i + 1, names, i, size - i - 1);
- }
- size--;
- return removedItem;
- }
- @Override
- public String toString() {
- String s = "";
- s += "[";
- boolean first = true;
- for (int i = 0; i < size; i++) {
- if (first) {
- s += names[i];
- first = false;
- } else {
- s += ", ";
- s += names[i];
- }
- }
- s += "]";
- return s;
- }
- /*
- * Removes and returns the first string in the list. If the list is empty,
- * null is returned.
- */
- public String removeFirst() {
- if (size < 1)
- return null;
- String removeItem = this.get(0);
- this.remove(0);
- return removeItem;
- }
- /*
- * Removes and returns the last string in the list. If the list is empty,
- * null is returned.
- */
- public String removeLast() {
- if (size < 1)
- return null;
- String removeItem = this.get(size - 1);
- this.remove(size - 1);
- size--;
- return removeItem;
- }
- /*
- * Removes all unused array elements from the end of the arry, if any exit.
- * After calling this method the size and the capacity of the list should be
- * same.
- */
- public void compress() {
- if (size == DEFAULT_CAPACITY)
- return;
- if (size == 0) {
- this.names = new String[] {};
- return;
- }
- String[] newList = new String[size];
- System.arraycopy(names, 0, newList, 0, size);
- this.names = newList;
- }
- /*
- * Increases the capacity, if needed, to new specified capacity. This may
- * mean making a new, lager array.
- */
- public void ensureCapacity(int newCapacity) {
- if (newCapacity <this.getCapacity()) {
- String message = "The new capacity must be lager than the original capacity("
- + this.getCapacity() + ").";
- throw new IndexOutOfBoundsException(message);
- }
- String[] newList = new String[newCapacity];
- if (size > 0)
- System.arraycopy(names, 0, newList, 0, size);
- this.names = newList;
- }
- /*
- * Returns the index of the first occurrence of specified string. If the
- * string is not in the list, returns -1.
- */
- public int getIndex(String s) {
- if (size == 0) {
- return -1;
- }
- for (int i = 0; i < size; i++) {
- if (get(i).equals(s)) {
- return i;
- }
- }
- return -1;
- }
- /*
- * Removes and returns the first occurrence of the specified string. If the
- * String is not in the list, returns null;
- */
- public String remove(String s) {
- for (int i = 0; i < size; i++) {
- if (get(i).equals(s)) {
- this.remove(i);
- return s;
- }
- }
- return null;
- }
- /* Removes all strings from the list and set the capacity to the default capacity.
- *
- * */
- public void Clear() {
- names = new String[DEFAULT_CAPACITY];
- size = 0;
- }
- }
- @Override
- public String toString() {
- if (size < 1)
- return "[]";
- StringBuilder readable = new StringBuilder(size * 7);
- String intro = "[";
- for (int i = 0; i < size; i++) { // T item: names
- readable.append(intro).append(names[i]);
- intro = ", ";
- }
- return readable.append(']').toString();
- }
Add Comment
Please, Sign In to add comment