Write a class called MyArrayList with the following features class header: publi
ID: 640618 • Letter: W
Question
Write a class called MyArrayList with the following features
class header:
public class MyArrayList implements MyCollection{
instance variables:
- private E[] list;
- private int size;
- private int capacity;
constructors:
- public MyArrayList() - Constructs an empty list with an initial capacity of 10;
initialize capacity to 10;
- public MyArrayList(int capacity) - Constructs an empty list with the specified
capacity
methods in the MyCollection interface
- public boolean add(E item) - Add the item to the end of the list. Use the
ensureCapacity method to double the capacity of the list array if size = capacity.
Increment size by 1 after adding the element. Always returns true.
- public void add(int index, E item) - Inserts the specified element at the
specified position in this list. Use the ensureCapacity method to double the
capacity of the list array if size = capacity.
Shifts the element currently at that position (if any) and any subsequent
elements one position to the right. Increment size by 1 after adding the
element.
The method throws an IndexOutOfBoundsException if index < 0 or index > size.
- public E get(int index) - Return the item at the specified position. The method
throws an IndexOutOfBoundsException if index < 0 or index ? size.HW2 Math 140 Due Date 2/25/2015 11:59 pm No Late Assignments
- public boolean remove(Object o) - Removes the first occurrence of the
specified element from this list, if it is present and then returns true. Shift the
array elements to the right of the removed element one position to the left.
Decrease size by 1.
If the list does not contain the element, it is unchanged. In this case, return
false.
Note: The remove method should not call the contains method.
- public int size()
Explanation / Answer
interface MyCollection<E> {
int size();
E get(int i);
boolean add(E obj);
void add(int i, E obj);
boolean remove(Object obj);
boolean contains(Object obj);
boolean isEmpty();
}
// The code in this class shows how to write the default constructor and
// suggestions for
// some of the other methods
class MyArrayList<E> implements MyCollection<E> {
private E[] list;
private int size;
private int capacity;
public MyArrayList() {
list = (E[]) new Object[10];
capacity = 10; // the list currently hold up to 10 objects
size = 0; // the list does not contain any objects
}
@Override
public boolean add(E item) {
/*
* (1) if size = capacity then call the ensureCapacity method to double
* the size of the list array
*
* (2) Insert the item at position = size.
*
* (3) Increase size by 1.
*/
if (size == capacity) {
ensureCapacity(size++);
}
list[size++] = item;
return true;
}
@Override
public void add(int i, E item) {
/*
* (1) if size = capacity then call the ensureCapacity method to double
* the size of the list array
*
* (2) Use the System.arraycopy method to shift the array elements
* starting at position i one element to the right. (3) Insert the item
* at position = i.
*
* (4) Increase size by 1.
*/
rangeCheck(i);
if (size == capacity) {
ensureCapacity(size + 1);
}
System.arraycopy(list, i, list, i + 1, size - i);
list[i] = item;
size++;
}
public void ensureCapacity(int minCapacity) {
/*
* if minCapacity is greater than capacity then
*
* (1) Declare an E[] array called temp. The length of temp =
* minCapacity. (2) Use System.arraycopy to copy the data stored in list
* to the temp array. (3) Set the list array to temp. (4) Set capacity =
* minCapacity.
*/
if (minCapacity > capacity) {
E[] temp = (E[]) new Object[minCapacity];
System.arraycopy(list, 0, temp, 0, size - 1);
list = temp;
capacity = minCapacity;
}
}
@Override
public boolean remove(Object obj) {
/*
* (1) Find the position (if any) of obj in the ArrayList. Do not use
* the contains method to determine if obj is in the list. (2) If obj is
* not in the list then return false (3) If obj is in the list then
* assume it's located at position i. Remove obj by shifting elements
* starting at position i+1 to the left one element. Use
* System.arraycopy to shift the array elements. Return true and
* decrease size by 1.
*/
for (int index = 0; index < size; index++)
if (obj.equals(list[index])) {
System.arraycopy(list, index + 1, list, index, size - index - 1);
size--;
return true;
}
return false;
}
@Override
public boolean contains(Object obj) {
/*
* (1) Use a while loop to determine if obj is in the list. If you find
* obj in the list then stop the while loop.
*
* (2) If list[i] is not equal to null then use the equals method to
* determine if it equals obj. Otherwise, if list[i] is null then
* determine if obj is null.
*/
boolean found = false;
int i = 0;
while (!found) {
if (list[i] != null && list[i].equals(obj)) {
found = true;
} else if (list[i] == null && obj == null) {
found = true;
}
i++;
}
return found;
}
@Override
public int size() {
return size;
}
@Override
public E get(int i) {
rangeCheck(i);
return this.list[i];
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return size == 0;
}
private void rangeCheck(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.