Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In Java Add methods get(i) and set(i,e) on top of ListStaticArray and ListDynamicArray. public class ListStaticArray { /* Instance variables */ private final int MAX_CAPACITY
In Java
Add methods get(i) and set(i,e) on top of ListStaticArray and ListDynamicArray.
public class ListStaticArray{ /* Instance variables */ private final int MAX_CAPACITY = 10000; private T[] data; private int size; private int capacity; /* Constructors */ public ListStaticArray() { this.capacity = MAX_CAPACITY; data = (T[])new Object[capacity]; size = 0; } public ListStaticArray(final int capacity) { this.capacity = capacity; data = (T[])new Object[capacity]; size = 0; } /* Operations */ /* Returns the number of elements in the array list */ public int size() { return size; } /* Returns whether the array is is empty */ public boolean isEmpty() { return size == 0; } /* Returns the capacity of dynamic array */ public int capacity() { return capacity; } /* Returns the capacity of */ /* add method - append an item to the end of array list */ public void add(T element) { ensureCapacity(); data[size] = element; size++; } /* add method - insert element at index i */ public void add(T element, int i) { ensureCapacity(); for (int k = size-1; k >= i; k--){ data[k+1] = data[k]; } data[i] = element; // assign the element to data[i] size++; } /* add method - append multiple items to the end of array */ public void add(T[] elements) { for(int k = 0; k < elements.length; k++) { add(elements[k]); } } /* remove method - remove at index i */ public T remove(int index) { if(index <= size-1) { T removed = data[index]; for (int k = index; k < size-1; k++) { data[k] = data[k+1]; } size--; return removed; } else { return null;} } /* remove method - remove from the end of array list */ public T removeTail() { T removed = data[--size]; return removed; } // static implementation private void ensureCapacity() throws IllegalStateException { if(capacity < size + 1 ) { throw new IllegalStateException ("The list is full"); } } /* Display array list */ public void printArray() { System.out.print("Array: "); if(size == 0) { System.out.print("EMPTY"); } else { for(int i = 0; i < size; i++) { System.out.printf("%s ", data[i]); } } System.out.print(" "); } /* Display the state of array list */ public void printState() { System.out.printf("\tCapacity: %d \tSize: %d ", capacity, size); } }
public class ListDynamicArray{ /* Instance variables */ private final int DEFAULT_CAPACITY = 3; private T[] data; private int size; private int capacity; /* Constructors */ public ListDynamicArray() { this.capacity = DEFAULT_CAPACITY; data = (T[])new Object[capacity]; size = 0; } public ListDynamicArray(final int capacity) { this.capacity = capacity; data = (T[])new Object[capacity]; size = 0; } /* Operations */ /* Returns the number of elements in the array list */ public int size() { return size; } /* Returns whether the array is is empty */ public boolean isEmpty() { return size == 0; } /* Returns the capacity of dynamic array */ public int capacity() { return capacity; } /* add method - append an item to the end of array list */ public void add(T element) { ensureCapacity(); data[size] = element; size++; } /* add method - insert element at index i */ public void add(T element, int i) { ensureCapacity(); for (int k = size-1; k >= i; k--){ data[k+1] = data[k]; } data[i] = element; // assign the element to data[i] size++; } /* add method - append multiple items to the end of array */ public void add(T[] elements) { for(int k = 0; k < elements.length; k++) { add(elements[k]); } } /* remove method - remove at index i */ public T remove(int index) { if(index <= size-1) { T removed = data[index]; for (int k = index; k < size-1; k++) { data[k] = data[k+1]; } size--; shrinkCapacity(); return removed; } else { return null;} } /* remove method - remove from the end of array list */ public T removeTail() { T removed = data[--size]; shrinkCapacity(); return removed; } // dynamic implementation private void ensureCapacity() { if(size == 0) { capacity = DEFAULT_CAPACITY; changeCapacity(); } if(size < capacity) { return; } this.capacity *= 2; // doubling the capacity changeCapacity(); } private void shrinkCapacity() { if(size > (capacity/2)) { return; } this.capacity /=2; changeCapacity(); } private void changeCapacity() { T[] newData = (T[]) new Object[capacity]; for(int i = 0; i < size; i++) { newData[i] = data[i]; } data = newData; } /* Display array list */ public void printArray() { System.out.print("Array: "); if(size == 0) { System.out.print("EMPTY"); } else { for(int i = 0; i < size; i++) { System.out.printf("%s ", data[i]); } } System.out.print(" "); } /* Display the state of array list */ public void printState() { System.out.printf("\tCapacity: %d \tSize: %d ", capacity, size); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started