In: Computer Science
. 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
import java.util.Iterator; import java.util.NoSuchElementException; public class ArrayList<E> { // instance variables /** Default array capacity. */ public static final int CAPACITY = 16; // default array capacity /** Generic array used for storage of list elements. */ private E[] data; // generic array used for storage /** Current number of elements in the list. */ private int size = 0; // current number of elements // constructors /** Creates an array list with default initial capacity. */ public ArrayList() { this(CAPACITY); } // constructs list with default capacity /** Creates an array list with given initial capacity. */ @SuppressWarnings({ "unchecked" }) public ArrayList(int capacity) { // constructs list with given capacity data = (E[]) new Object[capacity]; // safe cast; compiler may give // warning } public void addAll(ArrayList<E> l){ int listSize = l.size(); for(int i=0; i<listSize; i++) { add(size(), l.get(i)); } } public boolean remove(E element) { int listSize = size(); int index = -1; for(int i=0; i<listSize; i++) { if(get(i).equals(element)) { index = i; break; } } if(index != -1) { remove(index); return true; } else { return false; } } public boolean search(E element) { for(int i=0; i<size; i++) { System.out.println("Checking " + data[i]); if(data[i].equals(element)) { return true; } } return false; } public void removeRange(int fromIndex, int toIndex){ for(int i=toIndex - 1; i >= fromIndex; i--) { if((i >= 0) && (i < size)) { remove(i); } } } public void trimToSize() { resize(size); } // public methods public int size() { return size; } public boolean isEmpty() { return size == 0; } public E get(int i) throws IndexOutOfBoundsException { checkIndex(i, size); return data[i]; } public E set(int i, E e) throws IndexOutOfBoundsException { checkIndex(i, size); E temp = data[i]; data[i] = e; return temp; } /** * * Inserts the given element at the specified index of the list, shifting * all * * subsequent elements in the list one position further to make room. * * @param i * the index at which the new element should be stored * * @param e * the new element to be stored * * @throws IndexOutOfBoundsException * if the index is negative or greater than size() */ public void add(int i, E e) throws IndexOutOfBoundsException { checkIndex(i, size + 1); if (size == data.length) // not enough capacity resize(2 * data.length); // so double the current capacity for (int k = size - 1; k >= i; k--) // start by shifting rightmost data[k + 1] = data[k]; data[i] = e; // ready to place the new element size++; } /** * * Removes and returns the element at the given index, shifting all * subsequent * * elements in the list one position closer to the front. * * @param i * the index of the element to be removed * * @return the element that had be stored at the given index * * @throws IndexOutOfBoundsException * if the index is negative or greater than size() */ public E remove(int i) throws IndexOutOfBoundsException { checkIndex(i, size); E temp = data[i]; for (int k = i; k < size - 1; k++) // shift elements to fill hole data[k] = data[k + 1]; data[size - 1] = null; // help garbage collection size--; return temp; } // utility methods /** Checks whether the given index is in the range [0, n-1]. */ protected void checkIndex(int i, int n) throws IndexOutOfBoundsException { if (i < 0 || i >= n) throw new IndexOutOfBoundsException("Illegal index: " + i); } /** Resizes internal array to have given capacity >= size. */ @SuppressWarnings({ "unchecked" }) protected void resize(int capacity) { E[] temp = (E[]) new Object[capacity]; // safe cast; compiler may give // warning for (int k = 0; k < size; k++) temp[k] = data[k]; data = temp; // start using the new array } /** * * Produces a string representation of the contents of the indexed list. * This exists for debugging purposes only. * * @return textual representation of the array list */ public String toString() { StringBuilder sb = new StringBuilder("("); for (int j = 0; j < size; j++) { if (j > 0) sb.append(", "); sb.append(data[j]); } sb.append(")"); return sb.toString(); } public void printList() { System.out.println(); for (int i = 0; i < size; i++) { System.out.print(get(i) + " "); } System.out.println(); } public static void main(String[] args) { ArrayList<String> stringList1 = new ArrayList<>(3); stringList1.add(0, "Java"); stringList1.add(0, "with"); stringList1.add(0, "Structure"); stringList1.add(0, "Data"); stringList1.printList(); stringList1.remove(2); stringList1.printList(); stringList1.set(2, "Python"); stringList1.printList(); ArrayList<String> stringList2 = new ArrayList<>(3); stringList2.add(0, "using"); stringList2.add(1, "Collections"); stringList2.printList(); stringList1.addAll(stringList2); stringList1.printList(); stringList1.remove("Python"); stringList1.printList(); stringList1.removeRange(1, 3); stringList1.printList(); stringList1.trimToSize(); stringList1.printList(); } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.