ArrayList.java - package Projects.Project_3 public class...

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

package Projects.Project_3; public class ArrayList<T extends Comparable<T>> implements List<T>{ //Instance variables private T[] a; private int size; private int length; public ArrayList() { size=0; length=2; a = (T[]) new Comparable[length]; }//Constructor @Override public boolean add(T element) { if(element==null) { return false; } if(isFull()) { grow(); } for(int x=0; x<length; x++) { if(a[x]==null) { a[x]=element; size++; return true; } } return false; } public void grow() { T[] b=(T[]) new Comparable[length*2]; for(int x=0; x<length; x++) { b[x]=a[x]; } a=b; length*=2; } @Override public boolean add(int index, T element) { if(element==null || index>length || index<0) { return false; } if(isFull()) { grow(); } T[] b=(T[]) new Comparable[length]; for(int x=0; x<index; x++) { b[x]=a[x]; } b[index]=element; for(int x=index+1; x<length; x++) { b[x]=a[x-1]; } a=b; size++; return true; } public boolean isFull() { return size==length;
} @Override public void clear() { for(int x=0; x<size; x++) { a[x]=null; } size=0;; } @Override public boolean contains(T element) { for(T kek : a) { try { if(kek.equals(element)) { return true; } } catch(NullPointerException e) { } } return false; } @Override public T get(int index) {

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture