In Java, we use the ArrayList class to implement the functionality of resizable-arrays.
It implements the List interface of the collections framework.
Java ArrayList Vs Array
In Java, we need to declare the size of an array before we can use it. Once the size of an array is declared, it's hard to change it.
To handle this issue, we can use the ArrayList class. It allows us to create resizable arrays.
Unlike arrays, arraylists can automatically adjust their capacity when we add or remove elements from them. Hence, arraylists are also known as dynamic arrays.
Creating an ArrayList
Before using ArrayList, we need to import the java.util.ArrayList package first. Here is how we can create arraylists in Java:
ArrayList<Type> arrayList= new ArrayList<>();
Here, Type indicates the type of an arraylist. For example,
// create Integer type arraylist
ArrayList<Integer> arrayList = new ArrayList<>();
// create String type arraylist
ArrayList<String> arrayList = new ArrayList<>();
In the above program, we have used Integer not int. It is because we cannot use primitive types while creating an arraylist. Instead, we have to use the corresponding wrapper classes.
Example: Create ArrayList in Java
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// create ArrayList
ArrayList<String> languages = new ArrayList<>();
// Add elements to ArrayList
languages.add("codeswithpankaj");
languages.add("p4n");
languages.add("java");
System.out.println("ArrayList: " + languages);
}
}
Output
ArrayList: [codeswithpankaj, p4n, java]
In the above example, we have created an ArrayList named languages.
Here, we have used the add() method to add elements to the arraylist.
Basic Operations on ArrayList
The ArrayList class provides various methods to perform different operations on arraylists. We will look at some commonly used arraylist operations in this tutorial:
Add elements
Access elements
Change elements
Remove elements
1. Add Elements to an ArrayList
To add a single element to the arraylist, we use the add() method of the ArrayList class. For example,
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// create ArrayList
ArrayList<String> languages = new ArrayList<>();
// add() method without the index parameter
languages.add("Java");
languages.add("p4n");
languages.add("Python");
System.out.println("ArrayList: " + languages);
}
}
Output
ArrayList: [Java, p4n, Python]
In the above example, we have created an ArrayList named languages. Here, we have used the add() method to add elements to languages.
2. Access ArrayList Elements
To access an element from the arraylist, we use the get() method of the ArrayList class. For example,
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<String> learn = new ArrayList<>();
// add elements in the arraylist
animals.add("codes");
animals.add("with");
animals.add("pankaj");
System.out.println("ArrayList: " + learn);
// get the element from the arraylist
String str = learn.get(1);
System.out.print("Element at index 1: " + str);
}
}
Output
ArrayList: [codes, with, pankaj]
Element at index 1: with
In the above example, we have used the get() method with parameter 1. Here, the method returns the element at index 1.
3. Change ArrayList Elements
To change elements of the arraylist, we use the set() method of the ArrayList class. For example,
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<String> languages = new ArrayList<>();
// add elements in the array list
languages.add("Java");
languages.add("Kotlin");
languages.add("C++");
System.out.println("ArrayList: " + languages);
// change the element of the array list
languages.set(2, "JavaScript");
System.out.println("Modified ArrayList: " + languages);
}
}
Output
ArrayList: [Java, Kotlin, C++]
Modified ArrayList: [Java, Kotlin, JavaScript]
In the above example, we have created an ArrayList named languages. Notice the line,
language.set(2, "JavaScript");
Here, the set() method changes the element at index 2 to JavaScript.
4. Remove ArrayList Elements
To remove an element from the arraylist, we can use the remove() method of the ArrayList class. For example,
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<String> animals = new ArrayList<>();
// add elements in the array list
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
System.out.println("ArrayList: " + animals);
// remove element from index 2
String str = animals.remove(2);
System.out.println("Updated ArrayList: " + animals);
System.out.println("Removed Element: " + str);
}
}
Output
ArrayList: [Dog, Cat, Horse]
Updated ArrayList: [Dog, Cat]
Removed Element: Horse
Here, the remove() method takes the index number as the parameter. And, removes the element specified by the index number.
Iterate through an ArrayList
We can use the Java for-each loop to loop through each element of the arraylist. For example,
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
// creating an array list
ArrayList<String> Course = new ArrayList<>();
animals.add("java");
animals.add("C++");
animals.add("Ruby");
System.out.println("ArrayList: " + Course );
// iterate using for-each loop
System.out.println("Accessing individual Course : ");
for (String language : Course ) {
System.out.print(language);
System.out.print(", ");
}
}
}
Output
ArrayList: [java, C++, Ruby]
Accessing individual elements:
java, C++ , Ruby,
コメント