ArrayList Features In Java
Inherits – ArrayList inherits AbstractList class and implements List interface.
Index based – Arraylist Elements can be randomly accessed using index . Arraylist Index start with ‘0’.
Ordered – Arraylist Elements maintain insertion ordered .
Dynamic resizing – ArrayList increase size dynamically when more elements needs to be added than it’ current size.
Non synchronized – ArrayList is not synchronized, by default..
Duplicates allowed – We can add duplicate elements in arraylist.
null value – Arraylist can store null values.
Wrapper class – ArrayList can not be used for primitive types, like int,float, char, etc. We need a wrapper class for arraylist. Sort – Element is not sorted by default in arraylist.
How ArrayList Works in Java?
Arraylist in java is a dynamic array backing by a array.This backing array is shared by all operation of arraylist. When we add element in arraylist actually element added in this backing array.If there is not enough space to store element in arraylist. it will create new array with doble size and copy all element in this from previous now array and then add new element.
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
* will be expanded to DEFAULT_CAPACITY when the first element is added.
*/
transient Object[] elementData;
/**
* The size of the ArrayList
*
* @serial
*/
private int size;
Why ArrayList is better than Array?
How to create an ArrayList?
List<Integer> listOfint = new ArrayList<Integer>();
List<Integer> listWithInitCap = new ArrayList<Integer>(5);
Set<String> set = new HashSet<String>();
List<String> listFromCollection = new ArrayList<String>(set);
How to add elements to an ArrayList?
import java.util.ArrayList;
import java.util.List;
public class ArrayListAdd {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("amit");
list.add("shubham");
list.add("shivam");
System.out.println("list ="+list);
list.add(1, "anup");
System.out.println("list ="+list);
}
}
list =[amit, shubham, shivam]
list =[amit, anup, shubham, shivam]
How to Iterating over an ArrayList ?
2. forEach loop
3. Iterator
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ArrayListAdd {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("amit");
list.add("shubham");
list.add("shivam");
//using for loop
System.out.println("=========>> iterate arraylist using loop");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//using forEach
System.out.println("=======>> iterate arraylist using forEach");
for (String element : list) {
System.out.println(element);
}
//using iterate()
System.out.println("=======>> iterate ararylist using iterator()");
Iterator<String> listIterator = list.iterator();
while (listIterator.hasNext()) {
String element = listIterator.next();
System.out.println(element);
}
}
}
=========>> iterate arraylist using loop
amit
shubham
shivam
=======>> iterate arraylist using forEach
amit
shubham
shivam
=======>> iterate ararylist using iterator()
amit
shubham
shivam
How to Change an element in ArrayList
We can change element of arraylist using set() method. In set method we pass value with index on which position we want to change values. Let’s see this by example
import java.util.ArrayList;
import java.util.List;
public class ChangeElement {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("amit");
list.add("shubham");
list.add("shivam");
//display list
System.out.println("before changing element list is ="+ list);
//change element on index 1
list.set(1, "kapil");
//display list
System.out.println("after changing element list is ="+ list);
}
}
Output:
before changing element list is =[amit, shubham, shivam]
after changing element list is =[amit, kapil, shivam]
How to remove elements from ArrayList?
We can remove element from arraylist using remove(int index) method.Now will learn by using code
import java.util.ArrayList;
import java.util.List;
public class RemoveElementFromArraylist {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("amit");
list.add("ravi");
list.add("shubham");
list.add("shivam");
System.out.println("before remove element from list ="+ list);
list.remove(1);
System.out.println("after remove element from list ="+ list);
}
}
before remove element from list =[amit, ravi, shubham, shivam]
after remove element from list =[amit, shubham, shivam]
ArrayList Methods
Now we will discuss some important java arraylist methods. 1. 1. 1. add(E e) : This method Appends the givenelement to the end of this list.
2. add(int index, E element) : This method Inserts the given element at the given position in this list.
3. clear() : This method Removes all of the elements from this list.
4. clone() : This Method Returns a shallow copy of this ArrayList instance.
5. contains(Object o) : This Method Returns true if this list contains the given element.
6. get(int index) : This method Returns the element at the given position in this list.
7. indexOf(Object o) : Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
8. isEmpty() : This method returns true if this list contains no.
9. iterator() . This method returns an iterator over the elements in this list in proper sequence.
10. lastIndexOf(Object o) : This method returns the index of the last occurrence of the given element in this list, or -1 if this list does not contain the element.
11. listIterator() : This method returns a list iterator over the elements in this list (in proper sequence).
12. listIterator(int index) : this method returns a list iterator over the elements in this list (in proper sequence), starting at the given position in the list.
13. remove(int index) : This method removes the element at the given position in this list.
14. remove(Object o) : This method removes the first occurrence of the given element from this list, if it is present.
15. removeRange(int fromIndex, int toIndex) : This method removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive
16. set(int index, E element) : This method replaces the element at the specified position in this list with the specified element.
17. size() : This method returns the number of elements in this list.
18. sort(Comparator<? super E> c) : This method sorts this list according to the order induced by the given Comparator.
19. subList(int fromIndex, int toIndex) : This method returns a view of the portion of this list between the gievn fromIndex, inclusive, and toIndex, exclusive.
20. toArray() : This method returns an array containing all of the elements in this list in proper sequence (from first to last element).
21. toArray(T[] a) : This method returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
22. trimToSize() : This Method trims the capacity of this ArrayList instance to be the list’s current size.
Java ArrayList Examples
- Create ArrayList In Java
- Storing Java Object In ArrayList
- How To Convert ArrayList To Array
- How to Convert Array to ArrayList
- How To Shallow Copy Or Clone a ArrayList
- Reverse order of all elements of ArrayList
- How to add all elements of a list to ArrayList
- Swap elements of Java ArrayList example
- Remove All Element From Arraylist Example
- how To Remove Element of arraylist
- How to Remove List From Arraylist
- How to get sub list from ArrayList
- How to get element of Arraylist
- How to find does ArrayList contains elements or not
- How to shuffle elements in ArrayList
- How to count number of element in ArrayList
- How to replace element in ArrayList
- How to insert element in arraylist
- How to use retailAll in arraylist
References : Java Official Document on Collections