The purpose of using Arrays.asList() in Java is to convert an array into a fixed-size list. This method allows you to create a List from an array without needing to manually traverse the array. It takes the elements of the array as parameters and returns a list that reflects the contents of the array.
Here’s a simple example:
import java.util.Arrays;
import java.util.List;
public class ListInitializationDemo {
public static void main(String[] args) {
List<String> colorsList = Arrays.asList("red", "blue", "green");
System.out.print("Colors List : " + colorsList); // Output: [red, blue, green]
}
}
Keep in mind that the list returned by Arrays.asList() is fixed-size, meaning you cannot add or remove elements from it; attempting to do so will throw an UnsupportedOperationException.
