Java中的数组 asList() 方法及示例
Java.util.Arrays类的asList()方法用于返回由指定数组支持的固定大小的列表。此方法与 Collection.toArray() 结合,充当基于数组和基于集合的 API 之间的桥梁。返回的列表是可序列化的并实现了 RandomAccess。
Tip: This runs in O(1) time.
句法:
public static List asList(T... a)
参数:该方法接受需要转换为List的数组a 。这里……被称为可变参数,它是一个参数数组,其工作方式类似于对象数组参数。
Special Note: The type of array must be a Wrapper Class(Integer,Float, etc) in case of primitive data types(int, float,etc) , i.e you can’t pass int a[] but you can pass Integer a[]. If you pass int a[], this function will return a List
返回值:此方法返回指定数组的列表视图。
示例 1:
Java
// Java program to Demonstrate asList() method
// of Arrays class for a string value
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating Arrays of String type
String a[]
= new String[] { "A", "B", "C", "D" };
// Getting the list view of Array
List list = Arrays.asList(a);
// Printing all the elements in list object
System.out.println("The list is: " + list);
}
// Catch block to handle exceptions
catch (NullPointerException e) {
// Print statement
System.out.println("Exception thrown : " + e);
}
}
}
Java
// Java program to Demonstrate asList() method
// of Arrays class For an integer value
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating Arrays of Integer type
Integer a[] = new Integer[] { 10, 20, 30, 40 };
// Getting the list view of Array
List list = Arrays.asList(a);
// Printing all the elements inside list object
System.out.println("The list is: " + list);
}
// Catch block to handle exceptions
catch (NullPointerException e) {
// Print statements
System.out.println("Exception thrown : " + e);
}
}
}
Java
// Java Program to demonstrate asList() method
// Which returns fixed size list and
// throws UnsupportedOperationException
// if any element is added using add() method
// Importing required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating Arrays of Integer type
Integer a[] = new Integer[] { 10, 20, 30, 40 };
// Getting the list view of Array
List list = Arrays.asList(a);
// Adding another int to the list
// As Arrays.asList() returns fixed size
// list, we'll get
// java.lang.UnsupportedOperationException
list.add(50);
// Printing all the elements of list
System.out.println("The list is: " + list);
}
// Catch block to handle exceptions
catch (UnsupportedOperationException e) {
// Display message when exception occurs
System.out.println("Exception thrown : " + e);
}
}
}
The list is: [A, B, C, D]
示例 2:
Java
// Java program to Demonstrate asList() method
// of Arrays class For an integer value
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating Arrays of Integer type
Integer a[] = new Integer[] { 10, 20, 30, 40 };
// Getting the list view of Array
List list = Arrays.asList(a);
// Printing all the elements inside list object
System.out.println("The list is: " + list);
}
// Catch block to handle exceptions
catch (NullPointerException e) {
// Print statements
System.out.println("Exception thrown : " + e);
}
}
}
The list is: [10, 20, 30, 40]
示例 3:
Java
// Java Program to demonstrate asList() method
// Which returns fixed size list and
// throws UnsupportedOperationException
// if any element is added using add() method
// Importing required classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] argv) throws Exception
{
// Try block to check for exceptions
try {
// Creating Arrays of Integer type
Integer a[] = new Integer[] { 10, 20, 30, 40 };
// Getting the list view of Array
List list = Arrays.asList(a);
// Adding another int to the list
// As Arrays.asList() returns fixed size
// list, we'll get
// java.lang.UnsupportedOperationException
list.add(50);
// Printing all the elements of list
System.out.println("The list is: " + list);
}
// Catch block to handle exceptions
catch (UnsupportedOperationException e) {
// Display message when exception occurs
System.out.println("Exception thrown : " + e);
}
}
}
输出: