Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.mrpaul.ads.mh050.ps17;
- import java.util.Arrays;
- /* ****************************************************************
- *
- *
- * STARTER CODE
- *
- *
- **************************************************************** */
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- import structures.RandomAccessListI;
- /**
- * Describe here
- * @author insert here
- *
- */
- public class MyArrayList<E> implements RandomAccessListI<E>, Iterable<E> {
- public static final int CAPACITY_DEFAULT = 10; //initial default size of array
- public static final int CAPACITY_MULTIPLIER = 2; //initial default multiplier when resizing array
- private E[] store; //array
- private int numElements; //logical size of array
- /**
- * Constructor. Note the cast: We can't make arrays of a type parameter, so we make
- * an array of objects and cast it to E[]. Create new array with given size.
- * @param initialCapacity
- */
- @SuppressWarnings("unchecked")
- public MyArrayList(int initialCapacity){
- store = (E[]) new Object[initialCapacity];
- }
- /**
- * Create ArrayList with default capacity
- */
- @SuppressWarnings("unchecked")
- public MyArrayList(){
- numElements = 0;
- store=(E[])new Object[CAPACITY_DEFAULT];
- }
- /**
- * Adds an element to the array. Resize if necessary.
- */
- public MyArrayList<E> add(E toAdd) {
- if(checkResize()){
- resize();
- }
- store[numElements] = toAdd;
- numElements ++;
- return this;
- }
- /**
- * Get the logical size of the array
- */
- public int size() {
- return numElements;
- }
- /**
- * Checks whether the parameter element is in the array. Use an iterator.
- */
- public boolean contains(Object val) {
- ArrayListIterator iterator = new ArrayListIterator();
- while(iterator.hasNext()){
- if(iterator.next().equals(val)){
- return true;
- }
- }
- return false;
- }
- /**
- * Returns an Object[] of the LOGICAL SIZE of the instance variable array.
- * i.e. a perfectly sized array of elements.
- *
- */
- public Object[] toArray() {
- Object[] ret = new Object[numElements];
- for(int i = 0; i < numElements; i++){
- ret[i] = store[i];
- }
- return ret;
- }
- /**
- * Is the list empty?
- */
- public boolean isEmpty() {
- return numElements == 0;
- }
- /**
- * Compares logical size and physical size of array. Do we need a resize?
- * There must always be space for a new element.
- * @return true if resize is required (no capacity left)
- */
- private boolean checkResize(){
- return numElements == store.length;
- }
- /**
- * Create a new, larger array via Arrays.copyOf (google it) and copy everything
- * from the array into store. New array should be larger by a factor of CAPACITY_MULTIPLIER
- */
- private void resize(){
- store = Arrays.copyOf(store, store.length * CAPACITY_MULTIPLIER);
- }
- /**
- * Adds this element at the given index. Shift everything else and resize as necessary.
- * If trying to add an element with an invalid index, throw an IndexOutOfBoundsException. For
- * example, if you are trying to add an element at index 7 to an ArrayList with only 3 elements.
- * Adding an element at index 3 to an ArrayList of 3 elements should not throw an exception.
- */
- public MyArrayList<E> add(int i, E value) {
- if(i > numElements){
- throw new IndexOutOfBoundsException();
- }
- if(checkResize()){
- resize();
- }
- for(int j = store.length; j > i; j--){
- store[j] = store[j - 1];
- }
- store[i] = value;
- return this;
- }
- /**
- * Set the element at the given index to this value.
- * Throw an IndexOutOfBounds Exception if the index is out of range
- */
- public E set(int i, E value) {
- if(i >= numElements){
- throw new IndexOutOfBoundsException();
- }
- E ret = store[i];
- store[i] = value;
- return ret;
- }
- /**
- * Remove the element at the given index; shift everything else and resize as necessary.
- * Return the element that was removed from the list.
- * Throw an IndexOutOfBounds Exception if the index is out of range
- */
- public E remove(int i) {
- if(i >= numElements){
- throw new IndexOutOfBoundsException();
- }
- E ret = store[i];
- for(int j = i; j < numElements + 1; j++){
- store[j] = store[j+1];
- }
- numElements--;
- return ret;
- }
- /**
- * Return the element at the given index.
- * Throw an IndexOutOfBounds Exception if the index is out of range
- */
- public E get(int i) {
- if(i >= numElements){
- throw new IndexOutOfBoundsException();
- }
- return store[i];
- }
- /**
- * Remove the first appearance of the given value.
- * Return true if the list contained the specified element.
- */
- public boolean remove(E val) {
- for(int i = 0; i < store.length; i++){
- if(get(i).equals(val)){
- remove(i);
- return true;
- }
- }
- return false;
- }
- /**
- * Empty the array list. Think: Do you NEED to set everything to null?
- */
- public void clear() {
- for(int i = 0; i < store.length; i++) {
- store[i] = null;
- }
- numElements = 0;
- }
- /**
- * create and return a new instance of the inner class ArrayListIterator
- */
- public Iterator<E> iterator() {
- return new ArrayListIterator();
- }
- /**
- * This is an inner class that defines an iterator for the MyArrayList
- *
- */
- public class ArrayListIterator implements Iterator<E>{
- private int at = 0; //index of current element
- private boolean readyToDelete = false; //ready to remove an element?
- /**
- * Compare where we are to the logical size of the array. Is there a next element?
- * Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)
- */
- public boolean hasNext() {
- if(at < numElements){
- return true;
- }
- return false;
- }
- /**
- * If the iteration has no more elements, throw a NoSuchElementException
- * Otherwise, increment the current element tracker, flip your ready to delete flag, and
- * return the previous (current-1) element.
- */
- public E next() {
- if(!hasNext()){
- throw new NoSuchElementException();
- }
- at++;
- readyToDelete = !readyToDelete;
- return store[at - 1];
- }
- /**
- * Removes from the underlying collection the last element returned by this iterator.
- * This method can be called only once per call to next(). Again: A call to remove MUST
- * be preceded by a call to next. That's when we throw the exception.The behavior of an
- * iterator is unspecified if the underlying collection is modified while the iteration
- * is in progress in any way other than by calling this method.
- * <p>
- * If we've never before called next OR if we've already called remove since the last call to next,
- * throw an IllegalStateException
- * Else adjust your delete flag and remove the appropriate element.
- *
- * Remember, you can call an outer-class method via: OuterClass.this.method()
- *
- */
- public void remove() {
- if(!readyToDelete){
- throw new IllegalStateException();
- }
- readyToDelete = !readyToDelete;
- }
- }
- public String toString(){
- StringBuilder sb = new StringBuilder();
- for(E value : store){
- sb.append(value + ", ");
- }
- return sb.toString().substring(0, sb.toString().length() - 2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement