Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * CSE 205: <11333> T-TH 4:30-5:45
- * Assignment <Assignment02>
- * Papa Demba Diack ASU ID 1216303829 and Abdullah Mahfuz ASU ID 1217109439
- * Description: MyArrayList.java
- */
- import java.util.Arrays;
- import java.util.NoSuchElementException;
- public class MyArrayList implements MyList {
- private int capacity = 16;
- private Object[] array = new Object[capacity];
- private int size = 0;
- @Override
- public void add(Object o) {
- //Object[] pullArray = array;
- if (size == capacity)
- {
- ensureCapacity(capacity*2);
- }
- size++;
- Object[] arrayAdder = new Object[size];
- int count = 0;
- if (array.length != 0 && array[0] != null)
- {
- for (Object obj : array)
- {
- arrayAdder[count] = obj;
- count++;
- }
- }
- arrayAdder[size-1] = o;
- array = arrayAdder;
- }
- @Override
- public void insert(int index, Object o) {
- //Object[] pullArray = array;
- if(index<0 || index>array.length)
- throw new NoSuchElementException();
- Object[] newArray = new Object[size+1];
- int count = 0;
- boolean added = false;
- while (count < size+1)
- {
- if (count == index)
- {
- newArray[count] = o;
- added = true;
- } else {
- if (!added)
- {
- newArray[count] = array[count];
- } else {
- newArray[count] = array[count-1];
- }
- }
- count++;
- }
- size++;
- array = newArray;
- /*size++;
- Object[] arrayInserter = new Object[size+1];
- for (int i = size-1; i>index;i--){
- arrayInserter[i] = arrayInserter[i-1];
- }
- arrayInserter[index] = o;
- array = arrayInserter;*/
- }
- @Override
- public void remove(int index) {
- if(index<0 || index>=array.length)
- throw new NoSuchElementException();
- Object [] arrayRemover = array;
- for (int i = index+1;i<arrayRemover.length;i++) {
- arrayRemover[i-1] = arrayRemover[i];
- }
- // (arrayRemover.length)--;
- size--;
- }
- @Override
- public Object get(int index) {
- if(index<0 || index>=array.length)
- throw new NoSuchElementException();
- return array[index];
- }
- @Override
- public int size() {
- return size;
- }
- public void ensureCapacity(int minCapacity) {
- if (minCapacity < size)
- {
- return;
- } else if (minCapacity < 16) {
- capacity = 16;
- } else {
- capacity = minCapacity;
- }
- /*if (minCapacity < size)
- (minCapacity < 16)
- Object [] _newarray = new Object[minCapacity];
- for (int i = 0; i<minC && i < values.length; i++) {
- }
- /*check to see if array was overweritten, create final size, update with previous values
- * Object[] newArr = new Object[minCapacity];
- */
- }
- public void trimToSize() {
- ensureCapacity(size);
- }
- // Do not alter the code below
- @Override
- public MyListIterator getIterator() {
- return new MyArrayListIterator();
- }
- private class MyArrayListIterator implements MyListIterator {
- int currentIndex = - 1;
- @Override
- public Object next( ) {
- currentIndex++;
- return array[currentIndex];
- }
- @Override
- public boolean hasNext( ) {
- return currentIndex +1 < size;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement