Quick Reach
What is an ArrayList?
The ArrayList is a Java class that is inherited from (or extends) the AbstractList class. The ArrayList class implements the List interface.
Main points about the ArrayList Java:
- A Java ArrayList is a dynamic array; an array that can grow after it is created.
- The standard arrays are not dynamic i.e. once a standard array is created it cannot grow. These are of fixed size and you cannot add or remove elements after these are created whereas the ArrayList allows that.
- ArrayList in Java gives fast iteration.
- It also gives fast random access.
- It is not synchronized.
- The ArrayList provides more powerful insertion than the standard arrays.
- Search mechanism in ArrayList is better than standard arrays.
- The ArrayList is comparable to the vector class, in that it is the dynamic array as well as to the standard arrays.
- The ArrayList manipulation is slower as a lot of shifting occur.
An ArrayList size method example
How to use ArrayList class in Java Program?
In order to use the ArrayList class in your projects, you must import this class as follows:
Import Java.util.ArrayList;
Declaring an ArrayList
After importing the Java ArrayList class, it is time to declare an object of the ArrayList class. The default way is:
ArrayList array_name = new ArrayList();
An ArrayList “array_name” of the ten size is created. The ArrayList default constructor creates an array of the ten size.
Other methods of declaration by using other constructors
You may also use other constructors of the ArrayList to declare an array as follows:
(1) By specifying size:
ArraList array_name = new ArrayList (size)
An array with the given number in the integer will be created.
(2) Create ArrayList from the other collection type:
ArrayList array_name = new ArrayList ( Collection c) ;
Java ArrayList example
The following example shows creating an ArrayList object. It adds five elements and then a for loop is used to display the ArrayList elements. See the example and code by clicking the image or link below:
Experience this online
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
|
import java.util.*;
public class ArrayList_example {
public static void main(String []args) {
ArrayList Intarr = new ArrayList(5); //Declaring ArrayList
Intarr.add(10);
Intarr.add(20);
Intarr.add(30);
Intarr.add(40);
Intarr.add(50);
//Displaying array
for (int i=0;i<Intarr.size();i++){
System.out.println(Intarr.get(i));
}
}
}
|
The output will be:
10
20
30
40
50
A few useful ArrayList methods
Following are a few useful ArrayList methods with examples.
ArrayList add Example
The example below shows creating an ArrayList object. It adds elements to the specific index position by using the ArrayList add method.
Experience this online
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
|
import java.util.*;
public class ArrayList_example {
public static void main(String []args) {
ArrayList Intarr = new ArrayList(5); //Declaring ArrayList
Intarr.add(10);
Intarr.add(20);
Intarr.add(30);
Intarr.add(40);
Intarr.add(50);
Intarr.add(0, 0); //Adding array elements at specific index position
Intarr.add(3, 25);
//Displaying array
for (int i=0;i<Intarr.size();i++){
System.out.println(Intarr.get(i));
}
}
}
|
The output will be:
0
10
20
25
30
40
50
As you can see, two array elements though added at the last in above sequence, however, its position is controlled by giving the specific index positions.
ArrayList size method example
The following example shows how to use the ArrayList size method.
Experience this online
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
|
import java.util.*;
public class ArrayList_example {
public static void main(String []args) {
ArrayList Intarr = new ArrayList(5); //Declaring ArrayList
Intarr.add(10);
Intarr.add(20);
Intarr.add(30);
Intarr.add(40);
Intarr.add(50);
//Displaying ArrayList size
System.out.println(“The size of ArrayList is: “ + Intarr.size());
}
}
|
The output will be:
The size of ArrayList is: 5
ArrayList contains method example
The contains method of the ArrayList is used to search in the ArrayList.
The following example shows how to use the ArrayList contains method. The contains method returns a Boolean. The returned result will be true if the given term is found.
Experience this online
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
|
import java.util.*;
public class ArrayList_example {
public static void main(String []args) {
ArrayList Intarr = new ArrayList(5); //Declaring ArrayList
Intarr.add(10);
Intarr.add(20);
Intarr.add(30);
Intarr.add(40);
Intarr.add(50);
//Displaying ArrayList contains
System.out.println(“The term is found in ArrayList? “ + Intarr.contains(10));
System.out.println(“The term is found in ArrayList? “ + Intarr.contains(15));
}
}
|
The output will be:
The term is found in ArrayList? true
The term is found in ArrayList? false
Read Further: Java vector | Java arrays
Leave A Comment?