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

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...
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...
Using the implementation of the array based list given, write a CLIENT method (that is NOT...
Using the implementation of the array based list given, write a CLIENT method (that is NOT part of the class) called replace, that given a value and a position replaces the value of the element at that position. REMEMBER error checking public static void replace( List aList, int newValue, int position) public class ArraybasedList implements MyListInterface{ private static final int DEFAULTSIZE = 25; // Data members: private int currentSize; private int maxSize; private S[] elements; //default constructor has NO parameters...
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector...
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items.
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
Show the code how to add a node to the front of a list. Draw the...
Show the code how to add a node to the front of a list. Draw the picture also that goes with the code. NOW Show the code how to add a node to the end of a list. Draw the picture also that goes with the code. EXTRA CREDIT Show the code how to add a node to a specific spot in a list. Draw the picture also that goes with the code.
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items. Anytime the list becomes full, double the size of the array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT