//Name: Johnny A. Shaieb //Lab: 2 // ******************************************************** // Array-based implementation of the ADT list. // ********************************************************* public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object items[]; // an array of list items private int numItems; // number of items in list public ListArrayBased() { items = new Object[MAX_LIST]; numItems = 0; } // end default constructor public ListArrayBased(Object item) { //Just a guess // Add code items = new Object[MAX_LIST]; items[0] = item; numItems = 1; } // end constructor public ListArrayBased(ListArrayBased L) {// Copies contents of L onto current list // Add code System.out.println("Copy Constructor Debug: " + L.numItems); items = new Object[MAX_LIST]; for(int i = 0; i < L.numItems; i++) { System.out.println("for: " + i); items[i] = L.items[i]; } }// end of copy constructor public boolean isEmpty() { return (numItems == 0); } // end isEmpty public int size() { return numItems; } // end size public void removeAll() { // Creates a new array; marks old array for // garbage collection. System.out.println("Deleting the entire list"); items = new Object[MAX_LIST]; numItems = 0; } // end removeAll public void add(int index, Object item) throws ListIndexOutOfBoundsException { System.out.println(); if (numItems > MAX_LIST) { throw new ListException("ListException on add"); } // end if if (index >= 1 && index <= numItems+1) { // make room for new element by shifting all items at // positions >= index toward the end of the // list (no shift if index == numItems+1) System.out.println("Adding list item " + item + " at position "+ index); System.out.println("void add():: Before For loop"); for (int pos = numItems; pos >= index; pos--) { System.out.println("POS: " + pos + " INDEX: " + index); items[translate(pos+1)] = items[translate(pos)]; } // end for System.out.println("void add():: After For loop"); // insert new item System.out.println("INDEX: " + index + " TRANSLATE: " + translate(index)); items[translate(index)] = item; numItems++; } else { // index out of range throw new ListIndexOutOfBoundsException("ListIndexOutOfBoundsException on add"); } // end if System.out.println(); } //end add public void append(Object item) {// Appends item at the end of the current list // Add code System.out.println(); System.out.println("append():: NUMITEMS: " + numItems + " TRANSLATE: " + translate(numItems + 1)); items[translate(numItems + 1)] = item; numItems++; System.out.println(); } public Object get(int index) throws ListIndexOutOfBoundsException { if (index >= 1 && index <= numItems) { return items[translate(index)]; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on get"); } // end if } // end get public int contains(Object item) {// If item is contained in the current list, return its index // otherwise return -1 // Add code for(int i = 0; i < numItems; i++) { if(items[i].equals(item)) { return i; } } return -1; } public void remove(int index) throws ListIndexOutOfBoundsException { System.out.println(); if (index >= 1 && index <= numItems) { // delete item by shifting all items at // positions > index toward the beginning of the list // (no shift if index == size) System.out.println("Deleting list item at position "+ index); for (int pos = index+1; pos <= size(); pos++) { System.out.println("POS: " + pos + " POS - 1: " + (pos - 1) + " TRANS: " + translate(pos-1)); items[translate(pos-1)] = items[translate(pos)]; } // end for numItems--; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on remove"); } // end if System.out.println(); } //end remove public void delete(Object item) {// delete item from the list if it exists (specify position) // otherwise state that the item is not in the current list // Add code System.out.println(); boolean delete = false; for(int i = 0; i <= numItems; i++) { if(items[i].equals(item)) { remove(i + 1); //I think the remove function is weird //remove(i); delete = true; } } if(!delete) { System.out.println("ITEM> " + item + " is NOT FOUND"); } System.out.println(); } public void display() {// Print out each element of the list in sequence // Add code System.out.println(); for(int i = 0; i < numItems; i++) { System.out.println(items[i]); } System.out.println(); } private int translate(int position) { System.out.println(); System.out.println("translate():: position: " + position + " translated to: " + (position -1)); System.out.println(); return position - 1; } // end translate } // end ListArrayBased