Question
Project 4: MyArrayList Objectives Writing a genericclass Be very familiar with theArrayListclass Be very familiar with arrays Assignment in Java develop a genericclassnamedMyArrayListthat implements the
Project 4: MyArrayList
Objectives
- Writing a genericclass
- Be very familiar with theArrayListclass
- Be very familiar with arrays
Assignment
in Java develop a genericclassnamedMyArrayListthat implements the basic functionality of theArrayListclass.
Implement the following methods (and only the following methods) according to the descriptions given.
MyArrayList() Constructs an empty list with an initial capacity of ten.
MyArrayList(intinitialCapacity) Constructs an empty list with the specified initial capacity.
Other Methods Modifiers and Type Method Descriptions:
boolean add(Ee) Appends the specified element to the end of this list.
If there is not space, allocates twice as much memory as before. (Note, this is different than for ArrayList)
double capacity() Reveals how many elements can be stored before increasing the memory allocation.
(Note, this is not a valid method in the ArrayList class)
void clear() Removes all of the elements from this list.
boolean contains(Objecto) Returns true if this list contains the specified element.
E get(intindex) Returns the element at the specified position in this list, if index is valid, otherwise null.
(Note, this is different than for ArrayList)
int indexOf(Objecto) Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
boolean isEmpty() Returns true if this list contains no elements.
E remove(intindex) Removes the element at the specified position in this list and returns it if index is valid, otherwise it returns null.
(Note, this is different than for ArrayList)
boolean remove(Objecto) Removes the first occurrence of the specified element from this list, if it is present.
Returns true if this list contained the specified element.
int size() Returns the number of elements in this list.
Class Implementation Details
- In yourMyArrayListclass, store the elements in an array ofObjects.
- Note, you can not useArrayListin yourMyArrayListclass(including inheriting from it).
- Because the elements are stored asObjectobjects, cast them back to the generic type before returning them. To remove the following warning:
Note: MyArrayList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
- Place the following annotation just before the method that casts the element back to the generic type:
@SuppressWarnings("unchecked")
- When removing an element, shift all of the other elements down (so that there are no indices with invalid references with an index less than the size).
- Do NOT decrease the capacity after removing or clearing elements.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started