You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Most of the developers choose Arraylist over Array
ArrayList is a resizable-array implementation of the List interface.
It implements all optional list operations, and permits all elements, including null.
Array VS ArrayList
Array
Arrays is that they are of fixed length so if it is full you cannot add any more elements to it, likewise if there are number of elements gets removed from it the memory consumption would be the same as it doesn’t shrink.
ArrayList
ArrayList can dynamically grow and shrink after addition and removal of elements.
ArrayList class enables us to use predefined methods of it which makes our task easy.
ArrayList Example in Java
import java.util.*;
public class ArrayListExample {
public static void main(String args[]) {
/*Creation of ArrayList: I'm going to add String
*elements so I made it of string type */
ArrayList<String> obj = new ArrayList<String>();
/*This is how elements should be added to the array list*/
obj.add("Ajeet");
obj.add("Harry");
obj.add("Chaitanya");
obj.add("Steve");
obj.add("Anuj");
/* Displaying array list elements */
System.out.println("Currently the array list has following elements:"+obj);
/*Add element at the given index*/
obj.add(0, "Rahul");
obj.add(1, "Justin");
/*Remove elements from array list like this*/
obj.remove("Chaitanya");
obj.remove("Harry");
System.out.println("Current array list is:"+obj);
/*Remove element from the given index*/
obj.remove(1);
System.out.println("Current array list is:"+obj);
}
}
Output:
Currently the array list has following elements:[Ajeet, Harry, Chaitanya, Steve, Anuj]
Current array list is:[Rahul, Justin, Ajeet, Steve, Anuj]
Current array list is:[Rahul, Ajeet, Steve, Anuj]
Methods of ArrayList class
index
function name
description
example
1
add( Object o)
adds an object o to the arraylist at last position
obj.add("hello");
2
add(int index, Object o)
adds the object o to the array list at the given index
obj.add(2, "bye");
3
remove(Object o)
Removes the object o from the ArrayList
obj.remove("Chaitanya");
4
remove(int index)
Removes element from a given index
obj.remove(3);
5
set(int index, Object o)
updating an element. replaces the element present at the specified index with the object o.
obj.set(2, "Tom");
6
int indexOf(Object o)
Gives the index of the object o. If the element is not found in the list then this method returns the value -1.
int pos = obj.indexOf("Tom");
7
Object get(int index)
returns the object of list which is present at the specified index
String str= obj.get(2);
8
int size()
gives the size of the ArrayList – Number of elements of the list
int numberofitems = obj.size();
9
boolean contains(Object o)
checks whether the given object o is present in the array list if its there then it returns true else it returns false.
obj.contains("Steve");
10
clear()
for removing all the elements of the array list in one go