ArrayLists - Part 2
Introduction
In the previous Lesson, we looked at ArrayLists and how to perform basic operations on them, like creation, access, etc.
In this Unit, we shall learn some more operations like concatenation, slicing, etc. on ArrayLists. We will also learn how to convert Arrays to ArrayLists and vice-versa.
1. ArrayList Concatenation
To concatenate two ArrayLists, we use the addAll() method, which appends the second ArrayList at the end of the first one.
This does affect the existing ArrayList.
Syntax
arrList1.addAll(arrList2);
The concatenated ArrayList is stored in arrList1
Code
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<Integer> arrList1 = new ArrayList<>();
arrList1.add(5);
arrList1.add(10);
arrList1.add(15);
arrList1.add(20);
System.out.println("arrList1: " + arrList1); // [5, 10, 15, 20]
ArrayList<Integer> arrList2 = new ArrayList<>();
arrList2.add(25);
arrList2.add(30);
arrList2.add(35);
arrList2.add(40);
System.out.println("arrList2: " + arrList2); // [25, 30, 35, 40]
arrList1.addAll(arrList2);
System.out.println("arrList1 after concatenation: " + arrList1); // [5, 10, 15, 20, 25, 30, 35, 40]
}
}
Output:
arrList1: [5, 10, 15, 20]
arrList2: [25, 30, 35, 40]
arrList1 after concatenation: [5, 10, 15, 20, 25, 30, 35, 40]
2. Slicing ArrayList
The slicing of ArrayLists is done through the method subList(). It works similarly to the Arrays method copyOfRange().
Syntax
arrList.subList(startIndex, endIndex);
Here the slicing begins from the startIndex and ceases at the endIndex. The slice does not include endIndex. It yields a List, which is an interface. The conversion of the List to ArrayList can be done using the constructor of ArrayList by passing it as an argument to its constructor.
Code:
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<Integer> arrList = new ArrayList<>();
arrList.add(5);
arrList.add(10);
arrList.add(15);
arrList.add(20);
ArrayList<Integer> subArrList = new ArrayList<>(arrList.subList(1, 3)); // [10, 15]
System.out.println(subArrList);
}
}
Output:
[10, 15]
In the above code, The ArrayList arrList is declared and initialized. An argument arrList.subList(1, 3) has been passed to the ArrayList constructor to convert the List returned by the subList() method to an ArrayList.
3. Array to Array list conversion
The asList() method of the Arrays class is used to convert an Array into an ArrayList by passing it to the constructor of the ArrayList.
The asList() method returns a List.
Syntax
Arrays.asList(arr);
Let's see a few examples for converting Arrays to ArrayLists.
Example 1:
Code
import java.util.*;
class Main {
public static void main(String[] args) {
String[] sportsArr = {"Basketball", "Cricket", "Football"};
System.out.println("Type of sportsArr: " + sportsArr.getClass().getSimpleName());
ArrayList<String> sportsArrList = new ArrayList<>(Arrays.asList(sportsArr));
// converting Array to ArrayList
System.out.println("sportsArrList: " + sportsArrList); // [Basketball", "Cricket", "Football]
System.out.println("Type of sportsArrList: " + sportsArrList.getClass().getSimpleName());
}
}
Output:
Type of sportsArr: String[]
sportsArrList: [Basketball, Cricket, Football]
Type of sportsArrList: ArrayList
In the above code,
The Array sportsArr gets declared and initialized in it.
An ArrayList is created with the name sportsArrList
Argument Arrays.asList(sportsArr) gets passed to the constructor of ArrayList to convert the List returned by asList() into an ArrayList.
Example 2:
Code
import java.util.*;
class Main {
public static void main(String[] args) {
Integer[] agesArr = {5, 10, 15};
System.out.println("Type of agesArr: " + agesArr.getClass().getSimpleName());
ArrayList<Integer> agesArrList= new ArrayList<>(Arrays.asList(agesArr));
// converting Array to ArrayList.
System.out.println("agesArrList: " + agesArrList); // [5, 10, 15]
System.out.println("Type of agesArrList: " + agesArrList.getClass().getSimpleName());
}
}
Output:
Type of agesArr: Integer[]
agesArrList: [5, 10, 15]
Type of agesArrList: ArrayLi
In the above code,
The Array agesArr gets declared and initialized.
Only objects can be stored by ArrayLists, not primitive data types. Thus, in declaring agesArr, we used the wrapper class Integer, not the int.
Argument Arrays.asList(agesArr) has been passed to the constructor of ArrayList to convert the List returned by the asList() method into an ArrayList.
4. Converting ArrayList to Array
The toArray() method is used to convert ArrayList to Array.
The Syntax of it is:
arrList.toArray(arr);
Here,
arrList is an ArrayList
toArray() is the method of ArrayList class
arr is an array, to which the arrlist elements are copied.
Code
import java.util.*;
class Main {
public static void main(String[] args) {
ArrayList< Integer > agesArrList = new ArrayList < Integer >( );
agesArrList.add(5);
agesArrList.add(10);
agesArrList.add(15);
System.out.println("Type of agesArrList: " + agesArrList.getClass( ).getSimpleName( ));
Integer agesArr[] = new Integer[agesArrList.size()]; // new Integer[3]
agesArrList.toArray(agesArr);
System.out.println("agesArr: " + Arrays.toString(agesArr)); // [5, 10, 15]
System.out.println("Type of agesArr: " + agesArr.getClass( ).getSimpleName( ));
}
}
Output:
Type of agesArrList: ArrayList
agesArr: [5, 10, 15]
Type of agesArr: Integer[]
In the above code,
The ArrayList agesArrList is declared and initialized. In fact an ArrayList can hold only objects and not primitive types. Hence for declaring agesArr the wrapper class Integer is used instead of int to store the elements from agesArrList.
5. Comparison of Array and ArrayList
Comparison of Array and ArrayList |
Summary
ArrayList methods:
addAll() method is used for concatenation of ArrayLists.
subList() method is used to obtain the portion of an ArrayList.
Conversion between Arrays and ArrayLists:
Arrays.asList() method is used to convert an Array to ArrayList.
toArray() method of ArrayList is used to convert ArrayList into an Array.