Thepurpose of this lab is to understand how the ArrayListclass in the Java API is implemented
So we do NOT want to useArrayList class in this project
Create MyArrayList class which implements some basic methods of ArrayList
Your arrayList only takes care of type Integer
Create a main method in your MyArrayList class to test the methods you have created. (Remember: don't create a static method if you want to test your methods in the main)
An integer array holds the values in the background
But arrays are of fixed size in Java!
Solution:
Start with an integer array (of fixed size of course)
Create a new integer array of a greater size (like double in size), if needed, and copy everything to it
A method would be required to determine the need for such an operation
Like ArrayList, your MyArrayList has .size() that refers to the length of the ArrayList.
public final int size()
Your MyArrayList has a private method:
private void ensureCapacity(final int minCapacity)
Increases the capacity of the array, if necessary, to ensure that it can hold at least the number of elements specified by the argument
You can change your array size:
Arrays.copyOf(int[] original, int newLength)
Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length
Example:
mArray = Arrays.copyOf(mArray, mArray.length * 2);
Create MyArrayList class which implements the following basic methods of ArrayList:
private void ensureCapacity(final int minCapacity) { }
Increases the capacity of the array, if necessary, to ensure that it can hold at least the number of elements specified by the argument.
//Constructor #1
public MyArrayList()
Constructs an empty list with an initial capacity of ten
//Constructor #2
public MyArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity
public boolean add(Integer element)
Inserts the specified element at the end of the list, return true if it
succeeded
public Integer get(int index)
Returns the element at the specified position in this list
public Integer set(int index, Integer element)
Replaces the element at the specified position in this list, should return the removed element value
public Integer remove(int index)
Removes the element at index specified by the argument, return the removed element value.
public boolean remove(Integer element)
Find and Remove the first occurrence of the specified element from this list, if it is present.
public int size()
Returns the number of elements in this list.
public void clear()
Removes all of the elements from this list.
private void checkRange(final int index)
If the index is not valid in any of the methods (add, get, set, remove) just throw a new exception with the following statement:
throw new IndexOutOfBoundsException();
Size of the int[] is not necessarily equal to the size of the arrayList represented by MyArrayList
So keep the last updated size of the arrayList in a variable
Deliverable: MyArrayList.java
Use exactly the same class name, method names, and types as given
You may create a main method for your own tests
Do not use Javas ArrayList