Java Program to Convert a List to Array and Vice Versa
What are Arrays?
In Java, an arrays is a container object that holds a fixed number of values of a single type. The values can be of any type, including primitive types (such as int, char, and boolean) and reference types (such as objects).
In this Article, we will write a program to calculate average of the values of an array.
Array Class:
In Java, the java.util.Arrays class is a utility class that provides various methods for working with arrays. Here are some examples of the methods that are available in the Arrays class:
- sort: sorts the elements of an array in ascending order.
- binarySearch: searches for a specified element in an array using binary search.
- fill: fills an array with a specified value.
- equals: checks if two arrays are equal.
List Class :
In Java, the List interface is a part of the Java Collection Framework. It is an ordered collection of elements, where duplicate values are allowed. It is implemented by several classes such as ArrayList, LinkedList, and Vector. These classes provide various methods for adding, removing, and manipulating elements in the list. They also allow for easy iteration through the elements in the list.
Program to convert a List to Array :
import java.util.ArrayList; import java.util.List; public class Main{ public static void main(String[] args) { // Creating a List Listlist = new ArrayList<>(); list.add("apple"); list.add("banana"); list.add("cherry"); // Convert List to Array String[] array = list.toArray(new String[list.size()]); // Print the Array for (String element : array) { System.out.println(element); } } }
Output: apple banana cherry
Explanation:
In this program, we first create a List of Strings and add three elements to it. Then, we use the toArray() method to convert the List to an Array of Strings. Finally, we use a for-each loop to print the elements of the Array.
Program to Convert an Array to List :
import java.util.Arrays; import java.util.List; public class ArrayToList { public static void main(String[] args) { // Create an Array String[] array = {"apple", "banana", "cherry"}; // Convert Array to List Listlist = Arrays.asList(array); // Print the List for (String element : list) { System.out.println(element); } } }
Output: apple banana cherry
Explanation:
In this program, we first create an Array of Strings. Then, we use the Arrays.asList() method to convert the Array to a List of Strings. Finally, we use a for-each loop to print the elements of the List.
As previously mentioned, the list returned by Arrays.asList() is fixed-size and any modification to the list will throw an UnsupportedOperationException.
To convert an array to a modifiable list we can do :
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ArrayToList { public static void main(String[] args) { // Create an Array String[] array = {"apple", "banana", "cherry"}; // Convert Array to List Listlist = new ArrayList<>(Arrays.asList(array)); // Print the List for (String element : list) { System.out.println(element); } } }
Output: apple banana cherry
This creates a new ArrayList that contains the elements of the given array, and it’s modifiable.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment