How to create list using an Array in Java ?
Declaration :
1 |
public static <T> List<T> asList(T... a) |
Explanation :
Purpose | The java.util.Arrays.asList(T… a) returns a fixed-size list backed by the specified array. (Changes to the returned list “write through” to the array.) This method acts as bridge between array-based and collection-based APIs |
Parameters | a ===> This is the array by which the list will be backed. |
Return Value | This method returns a list view of the specified array. |
Exception | NA |
Java Program : Example
Below example will explain java.util.Arrays.asList() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.c4learn; import java.util.Arrays; import java.util.List; public class ArrayDemo1 { public static void main (String args[]) { // Create an array of strings String arr[] = new String[]{"A","B","C","D"}; List list1 = Arrays.asList(arr); // Print the list System.out.println("Value of Array :" + list1); } } |
Output of Program :
1 |
Value of Array :[A, B, C, D] |