Introduction
In this blog, you will learn how to convert an array to a list. There are several methods to perform this task. Some of the methods are as follow-
1. Arrays.asList() Method
import java.util.*;
class ArrayToListExample {
public static void main(String args[]) {
String arr1[] = {
"Abhishek",
"Ishika",
"Harshit",
"Shruti"
};
List < String > list = Arrays.asList(arr1);
System.out.println(list);
}
}
Output
2. Collections.addAll() Method
import java.util.*;
class ArrayToListExample {
public static void main(String args[]) {
String arr1[] = {
"Abhishek",
"Harshit",
"Shruti",
"Manish"
};
List < String > list = new ArrayList();
Collections.addAll(list, arr1);
System.out.println(list);
}
}
Output
3. Using for() loop
import java.util.*;
class ArrayToListExample {
public static void main(String args[]) {
String arr1[] = {
"Ram",
"Shyam",
"Mohan",
"Sohan"
};
List < String > list = new ArrayList();
for (int i = 0; i < arr1.length; i++) {
list.add(arr1[i]);
}
System.out.println(list);
}
}
Output
Conclusion
In this blog, we have seen how to convert an array to a list. Thanks for reading and I hope you liked it. If you have any suggestions or queries about this blog, please share your thoughts. You can read my other blog and articles by clicking here.
Happy learning, friends!