Question

In: Computer Science

1. Adapt the custom array list implementation code with the following changes: (a) Add code to...

1. Adapt the custom array list implementation code with the following changes:

(a) Add code to the ensureCapacity() method to print out a message including how many elements are copied to the new array on resizing

Array List Implementation:


public class MyArrayList<E> implements MyList<E> {

public static final int INITIAL_CAPACITY = 16;

private E[] data = (E[])new Object[INITIAL_CAPACITY];

private int size = 0; // Number of elements in the list

public MyArrayList() {
}


  
public MyArrayList(E[] objects) {
for (int i = 0; i < objects.length; i++)
add(objects[i]);
}

  
@Override
public void add(int index, E e) {   


if (index < 0 || index > size)
throw new IndexOutOfBoundsException
("Index: " + index + ", Size: " + size);

ensureCapacity();

for (int i = size - 1; i >= index; i--)
data[i + 1] = data[i];

data[index] = e;

  
size++;
}

  
private void ensureCapacity() {
if (size >= data.length) {


E[] newData = (E[])(new Object[size * 2 + 1]);

System.arraycopy(data, 0, newData, 0, size);

data = newData;
}
}

@Override
public void clear() {
data = (E[])new Object[INITIAL_CAPACITY];
size = 0;
}

@Override
public boolean contains(Object e) {
for (int i = 0; i < size; i++)
if (e.equals(data[i])) return true;

return false;
}

@Override
public E get(int index) {
checkIndex(index);
return data[index];
}

private void checkIndex(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException
("Index: " + index + ", Size: " + size);
}
  
@Override
public int indexOf(Object e) {
for (int i = 0; i < size; i++)
if (e.equals(data[i])) return i;

return -1;
}

@Override
public int lastIndexOf(E e) {
for (int i = size - 1; i >= 0; i--)
if (e.equals(data[i])) return i;

return -1;
}

@Override
public E remove(int index) {
checkIndex(index);
  
E e = data[index];

for (int j = index; j < size - 1; j++)
data[j] = data[j + 1];

data[size - 1] = null;

size--;

return e;
}

@Override
public E set(int index, E e) {
checkIndex(index);
E old = data[index];
data[index] = e;
return old;
}

@Override
public String toString() {
StringBuilder result = new StringBuilder("[");

for (int i = 0; i < size; i++) {
result.append(data[i]);
if (i < size - 1) result.append(", ");
}

return result.toString() + "]";
}

public void trimToSize() {
if (size != data.length) {
E[] newData = (E[])(new Object[size]);
System.arraycopy(data, 0, newData, 0, size);
data = newData;
}
}

@Override
public java.util.Iterator<E> iterator() {
return new ArrayListIterator();
}

  
private class ArrayListIterator
implements java.util.Iterator<E> {
private int current = 0;

@Override
public boolean hasNext() {
return (current < size);
}

@Override
public E next() {
return data[current++];
}

@Override
public void remove() {
MyArrayList.this.remove(current);
}
}
  
@Override
public int size() {
return size;
}
}

Solutions

Expert Solution

public class MyArrayList<E> implements MyList<E> {

   public static final int INITIAL_CAPACITY = 16;

   private E[] data = (E[])new Object[INITIAL_CAPACITY];

   private int size = 0; // Number of elements in the list

   public MyArrayList() {
   }


  
   public MyArrayList(E[] objects) {
       for (int i = 0; i < objects.length; i++)
           add(objects[i]);
   }

  
   @Override
   public void add(int index, E e) {   

       if (index < 0 || index > size)
           throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);

       ensureCapacity();

       for (int i = size - 1; i >= index; i--)
           data[i + 1] = data[i];

       data[index] = e;

       size++;
   }

   private void ensureCapacity() {
       if (size >= data.length) {
           E[] newData = (E[])(new Object[size * 2 + 1]);
           System.arraycopy(data, 0, newData, 0, size);
           // display the number of elements that are copied to the new array on resizing
           System.out.println("Copying "+size+" elements to new array");

           data = newData;
       }
   }

   @Override
   public void clear() {
       data = (E[])new Object[INITIAL_CAPACITY];
       size = 0;
   }

   @Override
   public boolean contains(Object e) {
       for (int i = 0; i < size; i++)
           if (e.equals(data[i]))
               return true;

       return false;
   }

   @Override
   public E get(int index) {
       checkIndex(index);
       return data[index];
   }

   private void checkIndex(int index) {
       if (index < 0 || index >= size)
           throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
   }
  
   @Override
   public int indexOf(Object e) {
       for (int i = 0; i < size; i++)
           if (e.equals(data[i]))
               return i;

       return -1;
   }

   @Override
   public int lastIndexOf(E e) {
       for (int i = size - 1; i >= 0; i--)
           if (e.equals(data[i]))
               return i;

       return -1;
   }

   @Override
   public E remove(int index) {
       checkIndex(index);
      
       E e = data[index];

       for (int j = index; j < size - 1; j++)
           data[j] = data[j + 1];

       data[size - 1] = null;

       size--;

       return e;
   }

   @Override
   public E set(int index, E e) {
       checkIndex(index);
       E old = data[index];
       data[index] = e;
       return old;
   }

   @Override
   public String toString() {
       StringBuilder result = new StringBuilder("[");

       for (int i = 0; i < size; i++) {
           result.append(data[i]);
           if (i < size - 1) result.append(", ");
       }

       return result.toString() + "]";
   }

   public void trimToSize() {
       if (size != data.length) {
           E[] newData = (E[])(new Object[size]);
           System.arraycopy(data, 0, newData, 0, size);
           data = newData;
       }
   }

   @Override
   public java.util.Iterator<E> iterator() {
       return new ArrayListIterator();
   }

   private class ArrayListIterator implements java.util.Iterator<E> {
       private int current = 0;

       @Override
       public boolean hasNext() {
           return (current < size);
       }

       @Override
       public E next() {
           return data[current++];
       }

       @Override
       public void remove() {
           MyArrayList.this.remove(current);
       }
   }
  
   @Override
   public int size() {
       return size;
   }
}


Related Solutions

. Implement your own custom linked list array implementation with the following changes: (a) Fill in...
. Implement your own custom linked list array implementation with the following changes: (a) Fill in the public E get(int index) method (b) Also add code to the get method to print out a message for each time an element in the list is checked while searching for the element You may want to study how the toString method goes from element to element in the list Java
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added....
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added. (DO NOT MODIFY THE HEADER FILE OR TEST FILE. only modify the list.cpp) /*LIST.CPP : */ #include "list.h" using namespace std; // Node class implemenation template <typename T> Node<T>::Node(T element) { // Constructor    data = element;    previous = nullptr;    next = nullptr; } // List implementation template <typename T> List<T>::List() {    head = nullptr;    tail = nullptr; } template...
Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
Add Instance to the Array List Instances in an Array Search for an Instance in the...
Add Instance to the Array List Instances in an Array Search for an Instance in the Array Delete an Instance from the Array Update an Instance in the Array Save Array Elements to CSV File Exit Program The requirement of this assignment is to design and implement a system in Java that will manage instantiated instances of the Objects created in the User Designed Object Assignment. This is the Young Adult Object. The system should be able to add an...
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // slist.cpp #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to...
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to the front of...
Make following changes. step(A) step 1 Implementation a Generic Linked List using the program developed in...
Make following changes. step(A) step 1 Implementation a Generic Linked List using the program developed in the class. step 2  Implement StackImpl class using the generic linked list. step 3 Test the program with different type and number of matching and un-matching brackets. This is how it works! When you run the class MatchBrackets a popup dialog appears asking you to select a file. You are provided two input files in the project. input1.txt contains matching brackets and input2.txt does not...
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT