Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Instantiating and Manipulating ArrayList in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.76k
    Comment on it

    In this blog we will learn about ArrayList. ArrayList is one of the collection class of collection framework. It is a way of storing elements dynamically in an array. It implements the List interface and extends the AbstractList class. Important points to remember about ArrayList class are as below:

    1- We can store duplicate elements in ArrayList

    2- Insertion order is maintained in ArrayList

    3- It is non synchronized

    4- As array works on index basis ArrayList allows random access of the elements.

    5- In case of removing any element from the ArrayList lot of shifting is done hence
        the manipulation process is slow.

    Hierarchically ArrayList class extends AbstractList class. The AbstractList class implements the List interface. The List interface inherits the Collection and Iterable interfaces.

    Syntax for declaration of ArrayList class:

    public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

    We implement more than one interface in java using ',' like above there are interface List, RandomAccess, Cloneable and Serializable.


    Constructors of Java ArrayList

     

    1- ArrayList(): Using this we build empty array list.

    2- ArrayList(Collection coll): Using this constructor we build array list that is
        initialized by elements of the “coll”.

    3- ArrayList(int size) : This constructor builds array list with a predefined size.


    Some important methods that we can use to manipulate the ArrayList are as below with the description:

    void add(int index, Object obj) : add method with two parameters is used to insert an element at specific position. Index denotes the position to which we want to insert the element i.e. obj.

     

    boolean addAll(Collection coll): This method is used to append all elements to the end of the list of collection in same order that they are return in.

     

    void clear(): This method will remove all elements in the list. There is no return type.

     

    int lastIndexOf(Object obj): It returns the index in the list of the specified element i.e. obj. The index will belong to the last occurrence of the specified element.

     

    Object[] toArray(): It returns an array that consists of the elements in the list in correct order.

     

    boolean add(Object obj): This method appends the specified element at the end of list.

     

    boolean addAll(int index, Collection coll): This method inserts all the elements in the specified collection into the list that invoked the method and the insertion will start at the position denoted by index.

     

    Object clone(): This method returns a copy of ArrayList.

     

    int indexOf(Object obj): This method returns -1 if the list that invoked the method does not return the specified element else it will return the index of the element that matches with the specified element. The index will be of the first match among the duplicates if any.

     

    void trimToSize(): ArrayList instance is resized using the trimTosize method.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: