Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Implement an ArrayList class that stores a list (of objects) using an array.

ID: 3718979 • Letter: 1

Question

1. Implement an ArrayList class that stores a list (of objects) using an array. Your class should have an append method to add an element to the list. Your main method should demonstrate the use of the class and method, on Integers or instances of whatever class you like. 2. Add a toString method that returns a String representation of an ArrayList and use it in your main method. 3. Add a prepend method to add an element at the front of an ArrayList 4. Make your class generic by adding a generic type parameter and using it appropriately throughout the code. 5. The following code is the start of the LinkedList class described in lecture. public class LinkedList protected class Node public Object data; public Node next; protected Node first; Add the prepend method that adds a new Object to the front of the list. 6. Add a method indexOf (Object e) that returns the index of the given object in the list, or 1 if the object is not in the list. 7. Add a method get (int i) that returns the object at the given index in the list, or throws an IndexOutOfBoundsException if there is no such element in the list. 8. Add an instance variable last and use it to implement an efficient (fast) append method that adds a new Object at the end of the list. 9. Make your class generic by adding a generic type parameter and using it appropriately throughout the code.

Explanation / Answer

Kindly refer to below code for ArrayList using Arrays.

I have added methods such as toString(), prepend(), getIndexOf(), append() and few more. Rest you can take reference and do it from your side to learn the working of this code.

-----------------------------------------------------------------------------------------------

MyArrayList.java

import java.util.Arrays;

public class MyArrayList {

    private Object[] myStore;
    private int actSize = 0;
   
    public MyArrayList(){
        myStore = new Object[10];
    }
   
    public Object get(int index){
        if(index < actSize){
            return myStore[index];
        } else {
            throw new ArrayIndexOutOfBoundsException();
        }
    }
   
    public void append(Object obj){
        if(myStore.length-actSize <= 5){
            increaseListSize();
        }
        myStore[actSize++] = obj;
    }
   
    public Object remove(int index){
        if(index < actSize){
            Object obj = myStore[index];
            myStore[index] = null;
            int tmp = index;
            while(tmp < actSize){
                myStore[tmp] = myStore[tmp+1];
                myStore[tmp+1] = null;
                tmp++;
            }
            actSize--;
            return obj;
        } else {
            throw new ArrayIndexOutOfBoundsException();
        }
       
    }
   
    public int size(){
        return actSize;
    }
   
    private void increaseListSize(){
        //System.out.println(" Increasing the length of array to store new elements");
        myStore = Arrays.copyOf(myStore, myStore.length*2);
    }
  
    public String toString() {
        //System.out.println(" Printing using toString method");
        String s = "";
        s = "" + myStore[0];
        for(int i = 1; i < myStore.length; i++)
            if(myStore[i] != null)
                s += " " + myStore[i];
        return s;
    }
  
    private int getIndexOf(Object e) {
        //System.out.println(" Getting the provided element's index");
        for (int i = 0; i < myStore.length; i++)
            if(myStore[i] != null && myStore[i].equals(e))
                return i;
      
        return -1;
    }
  
    public void prepend(int index, Object obj) {
        //Adding new element in front of array
        Object[] newMyStore = new Object[myStore.length];
        System.arraycopy(myStore, 0, newMyStore, 0, index);
        System.arraycopy(myStore, index, newMyStore, index + 1, myStore.length - index - 1);
        newMyStore[index] = obj;
        myStore = newMyStore;
    }
   
    public static void main(String a[]){
        MyArrayList myArrLst = new MyArrayList();
        myArrLst.append(new Integer(21));
        myArrLst.append(new Integer(15));
        myArrLst.append(new Integer(41));
        myArrLst.append(new Integer(63));
        myArrLst.append(new Integer(74));
        System.out.println(myArrLst.toString());
      
        myArrLst.append(new Integer(79));
        myArrLst.append(new String("abc"));
        System.out.println("Element at Index 5:"+myArrLst.get(5));
        System.out.println("List size: "+myArrLst.size());
        System.out.println("Removing element at index 2: "+myArrLst.remove(2));
        System.out.println(myArrLst.toString());
      
        System.out.println(myArrLst.getIndexOf(new String("abc")));
      
        myArrLst.prepend(0, new Integer(54));
        System.out.println(myArrLst.toString());
    }
}

-------------------------------------------------Output---------------------------------------------------------------------------

21 15 41 63 74
Element at Index 5:79
List size: 7
Removing element at index 2: 41
21 15 63 74 79 abc
5
54 21 15 63 74 79 abc

------------------------------------------------------------------------------