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

ArrayList<E> class Study ArrayList.java source code file that provides the imple

ID: 675006 • Letter: A

Question

ArrayList<E> class

Study ArrayList.java source code file that provides the implementation of the ArrayList class that we have been using. Study both the source code and the documentation and answer the following questions:

1. How many constructors are in the class?

2. The data is stored in an array. What is the name and the type of the array data field? What is the initial capacity of the array? What are the rules for growing the array? Is there a maximum size that this array can grow to?

3. What methods are involved in adding an element to the ArrayList (list all the methods that are called, including the ones from other classes)?

4. Think of how you would remove an element from an array (hint: we discussed it in class). How is your approach different than the one used by the remove method of the ArrayList class? 5. In answering the above questions you encountered the access modifier native. Explain what it means and when it is used.

Explanation / Answer

1.
Constructors in ArrayList class
Constructor :
Constructor has same name as Class name and used to instatiage the class object
There are three constructors of ArrayList

Default constructor
ArrayList();
Parameter Constructor
ArrayList(int capacity);
ArrayList(CollectionList);
A collectionList is list of objects of built in type or user defined type of class objects.
---------------------------------------------------------------------------------------------------------------
2.
Name of the array in the ArrayList is elementData
The type of elementData is Object type.
The initial size of the ArrayList is 10.
The maximum size of array is MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8
is declared in the ArrayList API
---------------------------------------------------------------------------------------------------------------
3.
Adding methods of ArrayList
public boolean add(E e) ;
public void add(int index, E element) ;
public boolean addAll(Collection<? extends E> c) ;
public boolean addAll(int index, Collection<? extends E> c);
---------------------------------------------------------------------------------------------------------------
4.
Removing an element from the array
Remove element at position.

Checking if the position is in the range of the size
Removing an element is override the elements of right side to the left by one
position and decrement the size of the array by one.

if(pos<0 || pos>SIZE)
   throws IndexOutOfRangeException();
else
   for(int i=pos;i<SIZE-1;i++)
   array[pos]=array[pos+1];


The remove method in ArrayList class that takes the position of the element
to remove then check the range check and calls copy method of System class

int numMoved = size - index - 1;
if (numMoved > 0)
          System.arraycopy(elementData, index+1, elementData, index,numMoved);

Then decrement the size by one and set the removed value to null.
--------------------------------------------------------------------------------------------------------------------------------------------------

5.

The native keyword is used to interact with system resoureces like memory, i/o and hardware which are written in C or C++ languages to work with system dependent functions to make those functions faster access rather implementing in java language. The native methods are written in other languages using C -language and are incorporated in java API (Application Programming Interface).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote