OrderedStringListType - import java.util.Iterator Holds a list of String objects always sorted in ascending [email protected] Zachary Latta public class

OrderedStringListType - import java.util.Iterator Holds a...

This preview shows page 1 - 3 out of 5 pages.

import java.util.Iterator; /** * Holds a list of String objects always sorted * in ascending order. * * @author Zachary Latta */ public class OrderedStringListType { private final int DEFAULT_CAPACITY = 10; private final int RESIZE_FACTOR = 2; private String[] list; private int elements; /** * Creates an empty list of the default capacity. */ public OrderedStringListType() { list = new String[DEFAULT_CAPACITY]; elements = 0; } /** * This constructor creates an empty list of the specified capacity. * * @param capacity The initial capacity. * @throws IllegalArgumentException if the specified capacity is less than one. */ public OrderedStringListType(int capacity) { if(capacity < 1) { throw new IllegalArgumentException(); } list = new String[capacity]; elements = 0; } /** * Adds a string to the list. * * @param str The string to add. */ public void add(String str) { // Check if there's anything in the list before checking against it. if(elements == 0) { addAtEnd(str); } else { boolean added = false; for(int i = 0; i < elements; i++) { if(str.compareTo(list[i]) < 0) { add(i, str);
Image of page 1
added = true; break; } } if(!added) { addAtEnd(str); } } } /** * Adds a string at a specified index.
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 5 pages?

  • Spring '14
  • @author, @param, @return, Zachary Latta

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture