ArrayList in Java



An ArrayList is a group of common elements. It is concrete implementation of List interface. ArrayList has ordered elements in which order they added. There are several methods which we will discuss here today.

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package net.raj.test;

import java.util.ArrayList;
import java.util.List;

public class ArrayListImpl {
 public static void main(String[] args) {
  // Declare an ArrayList
  List<string> list = new ArrayList<>();
   
  list.add("one");
  list.add("two");
  list.add("three");
  list.add("four");
  
  System.out.println("Sample ArrayList 1:: "+list.toString());
 }
}

Output:

Sample ArrayList 1:: [one, two, three, four]


In the above example we saw that we added few elements when print those items we saw the same order in which they added to list.
It shows the basic property of list of ordering.

On the performance front, comparison with Linkedlist
Fetch Operation : Arraylist performs better than Linkedlist if it comes to get the item from the list. It takes O(1) time i.e. Constant time to fetch or retrieve an element from the list.
Insert Operation : While insertion and deletion operation performs better with Linkedlist than Arraylist. For both type of list it takes O(1) average time but in worst case Arraylist takes O(n) time. The worst can be defined when the Arraylist is full and program adds another element in it then first it would copy the whole content to new Arraylist and add new element at tail which takes O(n) time.
Delete Operation : When we consider the removal of an element, it's a head-on between Linkedlist and Arraylist. Both takes O(n) time for this operation. It takes O(1) time for call of the methods remove(int index) and remove(object o) provided by List interface but involves copying elements from old array to new updated array, hence overall it runs in O(n) time.

Property
List interface keeps the order of insertion of elements i.e. the order in which the elements added to the list will remain in same order after any operation. Every addition takes the index of last inserted element plus one (last index + 1).

 List interface keeps the order of insertion of elements i.e. the order in which the elements added to the list will remain in same order after any operation. Every addition takes the index of last inserted element plus one (last index + 1).


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package net.raj.test;

import java.util.ArrayList;
import java.util.List;

public class ArrayListImpl {

    public static void main(String[] args) {
        // Declare an ArrayList
        List&lt;String&gt; list = new ArrayList&lt;&gt;();

        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");

        System.out.println("Sample ArrayList 1:: " + list.toString());

        // Reuse the same object
        // list keeps the order
        list = new ArrayList&lt;&gt;();

        list.add("three");
        list.add("four");
        list.add("one");
        list.add("two");

        System.out.println("Sample ArrayList 2:: " + list.toString());
    }
}

Output:
Sample ArrayList 1:: [one, two, three, four]
Sample ArrayList 1:: [one, two, three, four, five]

Traversal:
There are two ways to traverse the list - forward or backward. The list has given methods like next and previous.

We can iterate the list starting from a specific index too.

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package net.raj.test;

import java.util.ArrayList;
import java.util.List;

public class ListIteratorImpl {
 public static void main(String[] args) {
  // Declare List
  List<String> list = new ArrayList<>();
  // populate list
  list.add("one");
  list.add("two");
  list.add("three");
  list.add("four");
  list.add("five");

  // Traverse the list in forward direction
  java.util.ListIterator<String> iter = list.listIterator();
  System.out.println("Iterating list in forward direction...");
  while (iter.hasNext()) {
   System.out.println(iter.next());
  }
  // Traverse the list in backward direction
  System.out.println("Iterating list in backward direction...");
  while (iter.hasPrevious()) {
   System.out.println(iter.previous());
  }

  // Traverse the list from specified position
  iter = list.listIterator(2);
  System.out.println("Iterating list from specified position...");
  while (iter.hasNext()) {
   System.out.println(iter.next());
  }
 }
}


Output :
Iterating list in forward direction...
one
two
three
four
five
Iterating list in backward direction...
five
four
three
two
one
Iterating list from specified position...
three
four
five

Remove Element:
From the list we can remove the element in two ways.
1. Remove the element on the basis of object index or position :

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package net.raj.test;

import java.util.ArrayList;
import java.util.List;

public class ArrayListImpl {

 public static void main(String[] args) {
  // Declare an ArrayList
  List<String> list = new ArrayList<>();

  list.add("one");
  list.add("two");
  list.add("three");
  list.add("four");
  list.add("five");

  System.out.println("Sample ArrayList Before Removal :: " + list.toString());

  // remove the 1st element
  list.remove(0);
  System.out.println("Sample ArrayList After Removal :: " + list.toString());

 }
}


Output :
Sample ArrayList Before Removal :: [one, two, three, four, five]
Sample ArrayList After Removal :: [two, three, four, five]

2. Remove the element on the basis of object value :

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package net.raj.test;

import java.util.ArrayList;
import java.util.List;

public class ArrayListImpl {

 public static void main(String[] args) {
  // Declare an ArrayList
  List<String> list = new ArrayList<>();

  list.add("one");
  list.add("two");
  list.add("three");
  list.add("four");
  list.add("five");

  System.out.println("Sample ArrayList Before Removal :: " + list.toString());

  // remove the element with value three
  list.remove("three");
  System.out.println("Sample ArrayList After Removal :: " + list.toString());

 }
}


Output :

Comments

Popular posts from this blog

Custom Stack Implementation in java

List - Insertion order

Custom LinkedList Implementation