What is ArrayList?
ArrayList is a resizable-array implementation of the List interface.
It implements list operations, and permits all elements, including null.
In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.
How to perform Basic Operations in ArrayList ?
- creating object for ArrayList
- adding objects into ArrayList
- accessing objects based on index
- searching an object in ArrayList whether it is listed under this instance or not
- adding elements at specific index
- checking whether the ArrayList is empty or not
- getting object index
- Get the size of the ArrayList.
Basic Operations
public class MyBasicArrayList { public static void main(String[] a){ ArrayListTry it Yourselfal = new ArrayList (); //add elements to the ArrayList al.add("JAVA"); al.add("C++"); al.add("PERL"); al.add("PHP"); System.out.println(al); //get elements by index System.out.println("Element at index 1: "+al.get(1)); System.out.println("Does list contains JAVA? "+al.contains("JAVA")); //add elements at a specific index al.add(2,"PLAY"); System.out.println(al); System.out.println("Is arraylist empty? "+al.isEmpty()); System.out.println("Index of PERL is "+al.indexOf("PERL")); System.out.println("Size of the arraylist is: "+al.size()); } }
How to read all elements in ArrayList by using iterator?
By calling iterator() method we will get Iterator object, through which we can iterate all the elements of the ArrayList.
Also we can iterate the ArrayList based on index too.
Example
import java.util.ArrayList; import java.util.Iterator; public class ArrayListIterator { public static void main(String a[]){ ArrayListTry it Yourselfarrl = new ArrayList (); //adding elements to the end arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); Iterator itr = arrl.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } }
How to delete all elements from my ArrayList?
For deleting all objects from ArrayList at once by calling clear() method on ArrayList.
Example
import java.util.ArrayList; public class ClearMyArrayList { public static void main(String a[]){ ArrayListTry it Yourselfarrl = new ArrayList (); //adding elements to the end arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println("Actual ArrayList:"+arrl); arrl.clear(); System.out.println("After clear ArrayList:"+arrl); } }
How to copy ArrayList to array?
For copying all content of ArrayList to an array is done by calling toArray() method.
Example
import java.util.ArrayList; public class MyArrayListArray { public static void main(String a[]){ ArrayListTry it Yourselfarrl = new ArrayList (); arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println("Actual ArrayList:"+arrl); String[] strArr = new String[arrl.size()]; arrl.toArray(strArr); System.out.println("Created Array content:"); for(String str:strArr){ System.out.println(str); } } }
How to swap two elements in a ArrayList?
By calling Collections.swap() method we can swap two elements of the ArrayList.
We have to pass the indexes which we need to swap.
Example
import java.util.ArrayList; import java.util.Collections; public class MyArrayListSwap { public static void main(String a[]){ ArrayListTry it Yourselflist = new ArrayList (); list.add("Java"); list.add("Cric"); list.add("Play"); list.add("Watch"); list.add("Glass"); list.add("Movie"); list.add("Girl"); Collections.swap(list, 2, 5); System.out.println("Results after swap operation:"); for(String str: list){ System.out.println(str); } } }
How to reverse ArrayList content?
We can reverse the content by calling Collections.reverse() method.
We have to pass ArrayList instance to this method, which reverses the content.
Example
import java.util.ArrayList; import java.util.Collections; public class MyArrayListReverse { public static void main(String a[]){ ArrayListTry it Yourselflist = new ArrayList (); list.add("Java"); list.add("Cric"); list.add("Play"); list.add("Watch"); list.add("Glass"); Collections.reverse(list); System.out.println("Results after reverse operation:"); for(String str: list){ System.out.println(str); } } }
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling