ArrayList (1).java - public class ArrayList<T extends...

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

The preview shows page 1 - 3 out of 3 pages.
public class ArrayList<T extends Comparable<T>> implements List<T> {private T[] array;private int numElements;private boolean isSorted;public ArrayList() {array = (T[]) new Comparable[2];numElements = 0;isSorted = true;}public boolean add(T element) {if (element != null) {if (numElements >= array.length - 1)array = cloneArray();array[numElements] = element;numElements++;isSorted = isSorted();return true;}elsereturn false;}public boolean add(int index, T element) {if (element != null && index >= 0 && index < numElements) {if (numElements == array.length)array = cloneArray();for (int i = numElements; i > index; i--)array[i] = array[i - 1];array[index] = element;numElements++;returntrue;}elsereturn false;}public void clear() {array = (T[]) new Comparable[2];numElements = 0;}public boolean contains(T element) {boolean contains = false;for (int i = 0; i < numElements; i++) {if (element.compareTo(array[i]) == 0)contains = true;}return contains;}public T get(int index) {return array[index];}public int indexOf(T element) {int index = -1;if (isSorted) {
int i = numElements/2;int control = numElements/2;while (array[i] != element && control > 1) {

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 3 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags
Computer Science, Sort, Void type, public ArrayList, public class ArrayList, private T

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture