Step 3: ArrayListDemo Class



    ArrayListDemo class defines an ArrayList object and demonstrates the usage of various methods of 'ArrayList' class.

  1. Instance variables: Declare an ArrayList object reference variable of base type 'Integer'.

  2. Default constructor: Initialize ArrayList variable and ensure that the capacity of Arraylist is 10 atleast.

  3. addElements: Fill the ArrayList by adding random integers between 0 and 50. Use 'java.util.Random' class to get random numbers. Use the method of Arraylist
    boolean add(Object o)

  4. printList: Print all the elements in the ArrayList. Use 'size()' method of array to get the number of elements. Use the method of ArrayList
    Object get(int index)
    int size()

  5. changeElement:
    Use 'for each' loop to iterate through the list, use 'compareTo' method to find the number. Use 'set' method to set new element. The full signature of this ArrayList method is
    Object set(int index, Object element)

  6. removeListElement: Accept the integer to be removed from the user.
    Get the index of the given integer in the ArrayList using 'indexOf' method.
    Remove the element at the index position using 'remove' method, the signature is
    Object remove (int index)
    int indexOf(Object elem)

  7. clearList: Use the 'clear' method of ArrayList to remove all the elements from the list. Full signature is
    void clear()
  8. Method to print the size of cleared list