Question: Below is a code for an ArrayList. Can I get comments with operations in each method that I'm missing. And after ArrayList code is a
Below is a code for an ArrayList. Can I get comments with operations in each method that I'm missing.
And after ArrayList code is a code that will allow me to test ArrayList code. I just need to be checked if everything followos directiong for creating the class.
Instructions:
In this assignment you will write your own version of the ArrayList class. Write a class called MyArrayList with the following features Follow the guidelines in the Programming Standards document. class header: public class MyArrayList
private int size;
private int capacity; constructors: public MyArrayList(int capacity) - Constructs an empty list with the specified capacity; initializes the capacity instance variable. public MyArrayList() use the keyword, this, to call the first constructor with an input argument equal to 10. private method in MyArrayList private void ensureCapacity(int minCapacity) - Increases the capacity of the list array, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. (The two add methods will call ensureCapacity when size == capacity is true.) 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) - The method throws an IndexOutOfBoundsException if index < 0 or index > size. Otherwise, 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 to the right. Increment size by 1 after adding the element.
public E get(int index) - The method throws an IndexOutOfBoundsException if index < 0 or index size. Otherwise, return the item at the specified position.
public E remove(int index) The method throws an IndexOutOfBoundsException if index < 0 or index size. Otherwise, removes the element at the specified position in this list. Shifts any subsequent elements to the left. Return the element removed from the list and decrement size by 1.
public boolean remove(Object o) - Removes the first occurrence of the specified element from this list, if it is present and then returns true. If the list does not contain the element, it is unchanged. In this case, return false.
public void clear() Assign null to each value of list and set size to zero.
public E set(int index, E item) - The method throws an IndexOutOfBoundsException if index < 0 or index size. Otherwise, replaces the element at the specified position in this list with the specified item. The method returns the element previously at the specified position.
public int size() returns the current size. public boolean contains(Object o) return true if the object is in the list; otherwise return false.
public boolean isEmpty() return true if size = 0; otherwise return false.
public int indexOf(Object o) - returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element
----------------------------------------------------------------------------------------------
code:
public class MyArrayList
private E[] list;
private int size;
private int capacity;
public MyArrayList(int capacity) { // constructor
list = (E[]) new Object[capacity]; // empty list
this.capacity = capacity;
}
public MyArrayList() { // constructor
this(10); // to call the first constructor with an input argument equal
// to 10
}
private void ensureCapacity(int minCapacity) { // Increases the capacity of
// the list array, if
// necessary
if (minCapacity > size) {
E[] tmp = (E[]) new Object[minCapacity];
System.arraycopy(list, 0, tmp, 0, list.length);
capacity = minCapacity;
list = tmp;
}
}
// --------------------------------------------------------
public boolean isEmpty() { // return true if size = 0
return size == 0;
}
public int size() {
return size;
}
public E get(int i) {// The method throws an IndexOutOfBoundsException if
// index < 0 or index size
// Otherwise, return the item at the specified
// position
if (i < 0 || i >= size) {
throw new IndexOutOfBoundsException();
}
return list[i];
}
public boolean contains(Object o) {// return true if the object is in the
// list; otherwise return false
boolean found = false;
int index = 0;
while (index < size && !found) {
if (list[index] == null) {
if (o == null) {
found = true;
}
} else {
found = list[index].equals(o);
}
index++;
}
return found;
}
public boolean add(E item) {// increase capacity 2 times
if (size >= capacity) {
ensureCapacity(2 * capacity);
}
// System.out.println("size = " + size);
list[size++] = item;
return true;
}
public void add(int index, E item) { // The method throws an
// IndexOutOfBoundsException if
// index
// < 0 or index > size
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
if (size >= capacity) {
ensureCapacity(2 * capacity);
}
System.arraycopy(list, index, list, index + 1, size - index);
list[index] = item;
size++;
}
public boolean remove(Object o) {// Removes the first occurrence of the
// specified element from this list
// if it is present and then returns
// true
boolean removed = false;
int index = 0;
while (index < size && !removed) {
if (list[index] == null) {
if (o == null) {
removed = true;
}
} else {
removed = list[index].equals(o);
}
if (!removed) {
index++;
}
}
if (removed) {
System.arraycopy(list, index + 1, list, index, size - index - 1);
size--;
}
return removed;
}
public int indexOf(Object o) { // returns the index of the first occurrence
// of the specified element
// this list, or -1 if this list does not
// contain the element
for (int i = 0; i < size; i++) {
if (equals(o, list[i])) {
return i;
}
}
return -1;
}
private boolean equals(Object target, Object element) {
if (target == null) {
return element == null;
} else {
return target.equals(element);
}
}
public void clear() {// Assign null to each value of list and set size to
// zero
for (int i = 0; i < size; i++) {
list[i] = null;
}
size = 0;
}
public E set(int index, E element) {
// no need to check index; get will do it for us
E old = get(index);
list[index] = element;
return old;
}
public E remove(int index) {// The method throws an
// IndexOutOfBoundsException if index < 0 or
// index size
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
} else {
E element = get(index);
for (int i = index; i < size - 1; i++) {
list[i] = list[i + 1];
}
size--;
return element;
}}}
-----------------------------------------------------------------------
public interface MyCollection
boolean remove(Object o);
int size();
T get(int i);
boolean isEmpty();
boolean add(T item);
T set(int i, T j);
T remove(int i);
void clear();
void add(int i, T item);
boolean contains(T item);
int indexOf(Object i);
}
------------------------------------------------------------------------------
test code:
public class TestMyArrayList {
public static void display(MyCollection> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public static void main(String[] args){
MyCollection
System.out.println("The list is empty: " + arr.isEmpty());
for(int i = 0; i < 15; i++) {
(arr).add(i);
}
System.out.println("The list is empty: " + arr.isEmpty());
System.out.println(" 1st Display");
display(arr);
System.out.println();
try {
System.out.println(arr.get(-1));
}
catch(IndexOutOfBoundsException e) {
System.out.println("Negative index");
}
try {
System.out.println(arr.get(15));
}
catch(IndexOutOfBoundsException e) {
System.out.println("index too big");
}
try {
System.out.println(arr.set(-1, -1));
}
catch(IndexOutOfBoundsException e) {
System.out.println("Negative index");
}
try {
System.out.println(arr.set(15, 15));
}
catch(IndexOutOfBoundsException e) {
System.out.println("index too big");
}
try {
System.out.println(arr.remove(-1));
}
catch(IndexOutOfBoundsException e) {
System.out.println("Negative index");
}
try {
System.out.println(arr.remove(15));
}
catch(IndexOutOfBoundsException e) {
System.out.println("index too big");
}
System.out.println();
System.out.println(arr.remove(new Integer(100)));
System.out.println(arr.remove(new Integer(5)));
System.out.println(" 2nd Display");
display(arr);
arr.remove(13); // remove list[13]
System.out.println(" 3rd Display");
display(arr);
arr.set(12, new Integer(-5));
arr.set(10, new Integer(-2));
System.out.println(" 4th Display");
display(arr);
arr.clear();
System.out.println(" size after clear operation = " + arr.size());
System.out.println(" Create new object");
arr = new MyArrayList<>(8);
arr.add(0, new Integer(10));
arr.add(0, new Integer(8));
arr.add(1, new Integer(9));
System.out.println();
System.out.println(" 5th Display");
display(arr);
System.out.println("7 is in the list: " + arr.contains(new Integer(7)));
System.out.println("8 is in the list: " + arr.contains(new Integer(8)));
for(int i = 0; i < 10; i++) {
arr.add(2, new Integer(i));
}
System.out.println(" 6th Display");
display(arr);
System.out.println(" location of -6 " + arr.indexOf(-6));
System.out.println("location of 9: " + arr.indexOf(new Integer(9)));
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
