Arraylist overview
ArrayList is a java class which implements List interface
Main features of ArrayList
1) It uses indexing to store the elements sequentially
2) It allows duplicate elements
3) It is not synchronized
4) Elements from Arraylist can be accessed using index
5) We can also use iterator and ListIterator to access the elements
6) Elements in ArrayList can grow and shrink dynamically
We can create ArrayList using its constructor as below
- ArrayList<String> list = new ArrayList<String>();
ArrayList<String> list = new ArrayList<String>();
Example :
If we want to store the list of programming language names, we can use ArrayList as below
- package com.kb.collectionsEx;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Vector;
- public class ArrayList1 {
- public static void main(String[] args) {
- ArrayList<String> list = new ArrayList<String>();
- list.add(“Java”);
- list.add(“c++”);
- list.add(“python”);
- list.add(“Java”);
- System.out.println(list);
- }
- }
package com.kb.collectionsEx; import java.util.ArrayList; import java.util.List; import java.util.Vector; public class ArrayList1 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add(“Java”); list.add(“c++”); list.add(“python”); list.add(“Java”); System.out.println(list); } }
Note :
We can see that, duplicate elements are allowed and also insertion order is maintained.
Some of the important methods of ArrayList
1) add( Object o) :
This method adds the object to the arraylist.
Example :
- ArrayList<String> obj = new ArrayList<String>();
- obj.add("java");
ArrayList<String> obj = new ArrayList<String>(); obj.add("java");
This statement adds a string “java” in the arraylist at the end
2) add(int index, Object o):
This method adds the object to the array list at the given index.
- obj.add(2, "java");
obj.add(2, "java");
It will add the string “java” to the 2nd index (3rd position as the array list starts with index 0) of array list.
3) remove(Object o) :
Removes the object from the ArrayList.
- obj.remove("bad things");
obj.remove("bad things");
This statement will remove the string “bad things” from the ArrayList.
4) remove(int index) :
Removes element from a given index.
- obj.remove(2);
obj.remove(2);
It removes the element of index 2 (3rd element of the list as List starts with o).
5) set(int index, Object o) :
Used for updating an element
It replaces the element present at the specified index with the specified object.
- obj.set(2, "java");
obj.set(2, "java");
It replaces the 3rd element (index =2 is 3rd element) with the value “java”.
6) int indexOf(Object o) :
It gives the index of the given object
If the element is not found in the list then this method returns the value -1.
- int pos = obj.indexOf("java");
int pos = obj.indexOf("java");
This gives the index (position) of the string “java” in the list.
7) Object get(int index) :
It returns the object of list which is present at the specified index.
- String str= obj.get(2);
String str= obj.get(2);
This returns the string object stored at 3rd position (index 2).
If we have integer array list then the returned value should be stored in an integer variable.
8) int size() :
It gives the size of the ArrayList
In other words, returns the number of elements in the list.
- int numberofitems = obj.size();
int numberofitems = obj.size();
9) boolean contains(Object o) :
It checks whether the given object is present in the Arraylist, If it’s there then it returns true else it returns false.
- obj.contains("java");
obj.contains("java");
This returns true if the string “java” is present in the list else returns false.
10) clear() :
This is used to remove all the elements of the array list
- obj.clear();
obj.clear();
This removes all the elements of ArrayList.
When should we use ArrayList ?
when we want to have index based access
When we have requirement to add elements only at the end of list
When we don’t care about duplicate elements
When we should not use ArrayList ?
When we need to add elements at the beginning or any specific position of list
When we need to avoid duplicates as it allows duplicate elements
When we don’t need retrieval based on index.