ArrayList Example Methods Java

ArrayList Example Methods Java

Note: Before going throuhg this "ArrayList Example Methods Java", it is adivised to read first ArrayList Introduction where all the methods, class signature, constructors and history are given.

4 programs are given on Arraylist.

  1. ArrayList Methods: In this program, the basic operations on array list are performed like addition, checking the list is empty or not, removing the elements, knowing the index number of an element etc.
  2. ArrayList Iterators: In this example, different styles of retrieving and printing the array list elements are described.
  3. ArrayList Operations: In this program, operations like reversing, sorting, searching, swapping, shuffling, filling, knowing maximum and minimum values are performed. Finally, an array of array lists is created and the elements are printed.
  4. ArrayList Special: In this program, operations like converting array list to array, array list to string, copying elements of one array into another, converting array to array list and finally cloning of an array list are performed.
This is the first program of the above four – ArrayList Example Methods Java


ArrayList Example Methods Java
Output screenshot of ArrayList Example Methods Java

ArrayList al1 = new ArrayList();
System.out.println(“isEmpty() before elements addition: ” + al1.isEmpty());
al1.add(10);

A non-generic (stores heterogeneous elements) ArrayList object al1 is created and checked for its emptiness with isEmpty(). This method returns true if no elements exist in the ArrayList, else false. The above statement returns true as still elements are not added to ArrayList. Later, with add() method elements are added.

al1.remove(1);
al1.remove(true);
System.out.println(“No. of elements after remove() two times: ” + al1.size());

The remove() method is overloaded that takes index number (as int) as parameter and also the name (as object) of the element. As two times the remove() method is called, the size (number of elements present) will be decreased by 2. Observe the screenshot.

Observe, the element 10 is added twice as ArrayList accepts duplicates. Its index number is retrieved using indexOf() and lastIndexOf() methods as in String. The contains() method returns true if the element exist, else false.

Object obj = al1.get(2);
Integer i1 = (Integer) obj;
int k = i1.intValue();
System.out.println(“\nProduct: ” + k*k);

The get() method, inherited from List interface, returns the element in object of Object class form. For printing it is okay, but object cannot be used in arithmetic operations. For this, the object must be converted into primitive data type form; this is what is done in the above code. Object obj is explicitly type casted to Integer class and from Integer, int value is obtained with intValue() method. A beginner should be acquainted with this type of conversions and are discussed elaborately in wrapper classes.

3 thoughts on “ArrayList Example Methods Java

  1. p.pavan

    Object obj=al.get(3);
    System.out.println(obj);
    Integer i1=new Integer(obj);
    int k=i1.intValue();
    System.out.println(k);

    What is the problem with this conversition sir it is giving an error??

Leave a Reply

Your email address will not be published. Required fields are marked *