Package edu.ncsu.csc217.collections.list
Class SortedList<E extends java.lang.Comparable<E>>
- java.lang.Object
-
- edu.ncsu.csc217.collections.list.SortedList<E>
-
- Type Parameters:
E- Any object that can be stored in a list.
public class SortedList<E extends java.lang.Comparable<E>> extends java.lang.ObjectImplementation of an array list extending AbstractList that only contains elements in sorted order. The elements of the list should implement theComparableinterface. Additionally, no duplicate elements are allowed to be stored in the list. The Javadoc is customized from Oracle'sArrayListJavadoc to fit the sorted use case. The SortedList has restricted functionality for working with an array list to meet the sorted use case.- Author:
- Dr. Sarah Heckman
-
-
Constructor Summary
Constructors Constructor Description SortedList()Constructs an empty list with an initial capacity of ten.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(E element)Adds the element to the list in sorted order.voidclear()Removes all of the elements from this list.booleancontains(E other)Returns true if this list contains the specified element.booleanequals(java.lang.Object obj)Compares the specified object with this list for equality.Eget(int index)Returns the element at the specified position in the list.inthashCode()Returns the hash code value for this list.intindexOf(E element)Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.booleanisEmpty()Returns true if the list contains no elements.Eremove(int index)Returns the element at the specified position in this list.intsize()Returns the number of elements in this list.
-
-
-
Method Detail
-
add
public boolean add(E element)
Adds the element to the list in sorted order. If the element does not implement theComparableinterface, then the element is added to the end of the list.- Parameters:
element- element to be added to the list- Returns:
- true if the element can be added to the list
- Throws:
java.lang.NullPointerException- if the specified element is nulljava.lang.IllegalArgumentException- if the specified element is a duplicate of an element already in the list
-
get
public E get(int index)
Returns the element at the specified position in the list.- Parameters:
index- index of the element to return- Returns:
- the element at the specified position in the list
- Throws:
java.lang.IndexOutOfBoundsException- if the index is out of range (index < 0 || index >= size())
-
remove
public E remove(int index)
Returns the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.- Parameters:
index- the index of the element to be removed- Returns:
- the element previously at the specified position
- Throws:
java.lang.IndexOutOfBoundsException- if the index is out of range (index < 0 || index >= size())
-
indexOf
public int indexOf(E element)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. This implementation first gets a list iterator (with listIterator()). Then, it iterates over the list until the specified element is found or the end of the list is reached.- Parameters:
element- element to search for- Returns:
- the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element
- Throws:
java.lang.NullPointerException- if the specified element is null
-
clear
public void clear()
Removes all of the elements from this list. The list will be empty after this call returns. This implementation calls removeRange(0, size()).
-
size
public int size()
Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.- Returns:
- the number of elements in this list.
-
isEmpty
public boolean isEmpty()
Returns true if the list contains no elements.- Returns:
- true if the list contains no elements.
-
contains
public boolean contains(E other)
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).- Parameters:
other- element whose presence in the list is to be tested- Returns:
- true if this list contains the specified element
-
hashCode
public int hashCode()
Returns the hash code value for this list. This implementation uses exactly the code that is used to define the list hash function in the documentation for the List.hashCode() method.- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This implementation first checks if the specified object is this list. If so, it returns true; if not, it checks if the specified object is a list. If not, it returns false; if so, it iterates over both lists, comparing corresponding pairs of elements. If any comparison returns false, this method returns false. If either iterator runs out of elements before the other it returns false (as the lists are of unequal length); otherwise it returns true when the iterations complete.- Overrides:
equalsin classjava.lang.Object
-
-