- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Arrays asList() Method
Description
The Java 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.
Declaration
Following is the declaration for java.util.Arrays.asList() method
public static <T> List<T> asList(T... a)
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
Getting List of String from Array Example
The following example shows the usage of Java Arrays asList() method. First, we've created an array of Strings and then used asList() method to create a corresponding list and then the list is printed.
package com.tutorialspoint; import java.util.Arrays; import java.util.List; public class ArrayDemo { public static void main (String args[]) { // create an array of strings String a[] = new String[]{"abc","klm","xyz","pqr"}; List<String> list = Arrays.asList(a); // printing the list System.out.println("The list is:" + list); } }
Output
Let us compile and run the above program, this will produce the following result −
The list is:[abc, klm, xyz, pqr]
Getting List of Integers from Array Example
The following example shows the usage of Java Arrays asList() method. First, we've created an array of Integer and then used asList() method to create a corresponding list and then the list is printed.
package com.tutorialspoint; import java.util.Arrays; import java.util.List; public class ArrayDemo { public static void main (String args[]) { // create an array of integers Integer a[] = new Integer[]{1, 2, 3, 4}; List<Integer> list = Arrays.asList(a); // printing the list System.out.println("The list is:" + list); } }
Output
Let us compile and run the above program, this will produce the following result −
The list is:[1, 2, 3, 4]
Getting List of Objects from Array Example
The following example shows the usage of Java Arrays asList() method. First, we've created an array of Student objects and then used asList() method to create a corresponding list and then the list is printed.
package com.tutorialspoint; import java.util.Arrays; import java.util.List; public class ArrayDemo { public static void main (String args[]) { // create an array of students Student a[] = new Student[]{new Student(1, "Julie"), new Student(2, "Robert"), new Student(3, "Adam")}; List<Student> list = Arrays.asList(a); // printing the list System.out.println("The list is:" + list); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } }
Output
Let us compile and run the above program, this will produce the following result −
The list is:[[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
To Continue Learning Please Login
Login with Google