- Java ArrayList class uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.
- ArrayList supports dynamic array which can grow as needed.
- Size of ArrayList can be dynamically increased or decreased.
- Java ArrayList class can contain duplicate elements.
- Java ArrayList class maintains insertion order of elements.
- Java ArrayList class is non synchronized.
- Java ArrayList allows random access because array works at the index basis.
- Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.
class ArrayList <T>
We can create ArrayList for storing String type elements as follows:
ArrayList <String> hs=new ArrayList <String> ();
We can create ArrayList for storing Integer type elements as follows:
ArrayList <Integer> hs=new ArrayList <Integer> ();
Constructors of ArrayList class as show below:
- ArrayList (): It builds an empty array list.
- ArrayList (Collection c): It builds an array list which is initialized with elements of the collection c.
- ArrayList (int capacity): It builds arraylist which has specified initial capacity .
Methods of ArrayList Class
Method | Description |
void add(int position, element obj) | It inserts specified element at the specified position in the ArrayList. |
boolean add(element obj) | It appends specified element to the end of the ArrayList. |
boolean addAll(Collection c) | It appends all the elements of the collection to the end of the ArrayList. |
element remove(int position) | It removes specified element at the specified position in the ArrayList. |
boolean remove(object obj) | It removes first occurrence of specified element obj from the ArrayList. |
void clear() | It removes all the elements from the ArrayList. |
boolean contains(Object o) | It returns true if ArrayList contains the specified element . |
object get(int position) | It returns the element at the specified position in the ArrayList. |
int indexOf(Object o) | It returns first occurrence of the specified element in the list or -1 if element not found in the list. |
int lastIndexOf(Object o) | It returns the last occurrence of the specified element in the list or -1 if the element is not found in the list. |
int size() | It returns the number of elements in the list. |
Example of java ArrayList class
import
java.util.Iterator;
import
java.util.ArrayList;
public
class
MyArrayList {
public
static
void
main(String a[]){
ArrayList
<String> al =
new
ArrayList
<String>();
//adding elements to the end
al
.add(
"A"
);
al
.add(
"B"
);
al
.add(
"C"
);
al
.add(
"D"
);
Iterator<String> itr = al.iterator();
while
(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Output:
A
B
C
D
Download this example from Here
Another example of java ArrayList class
import
java.util.ArrayList;
public
class
MyArrayListOperation {
public
static
void
main(String[] a){
ArrayList<String> al =
new
ArrayList<String>();
//add elements to the ArrayList
al.add(
"C"
);
al.add(
"C++"
);
al.add(
"java"
);
al.add(
"PHP"
);
System.out.println(
"Size of the arraylist is: "
+al.size());
System.out.println(al);
//get elements by index
System.out.println(
"Element at index 1: "
+al.get(2
));
//add elements at a specific index
al.add(
2
,
".Net"
);
System.out.println(al);
System.out.println(
"Is arraylist empty? "
+al.isEmpty());
System.out.println(
"Index of PERL is "
+al.indexOf(
"Java"
));
}
}
Output:
Size of the arraylist is: 4
[C, C++, Java, PHP]
Element at index 2: Java
[C, C++,.Net ,Java, PHP]
Is arraylist empty? false
Index of Java is 3
[…] ArrayList internally uses dynamic array to store the […]
LikeLike
[…] ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same […]
LikeLike
[…] 1) ArrayList implements List Interface . […]
LikeLike
nice
LikeLiked by 1 person