Java中的集合checkedList()方法和示例
Collections 类的checkedList()方法存在于Java.util 包中,用于返回指定列表的动态类型安全视图。这里要注意的关键是,如果指定的列表是可序列化的,则返回的列表将是可序列化的。由于 null 被认为是任何引用类型的值,因此返回的列表允许在后备列表中插入 null 元素。
Tip: This method is compatiable with java version 1.5 and onwards.
句法:
public static List
checkedList(List list, Class type)
参数:此方法采用以下参数作为参数:
- 要为其返回动态类型安全视图的列表
- 列表允许持有的元素类型
返回类型:指定列表的动态类型安全视图。
异常:此方法抛出ClassCastException
示例 1:
Java
// Java program to Demonstrate checkedList() method
// of Collections class for a string value
// 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 an ArrayList of string type by
// declaring object of List
List arlst = new ArrayList();
// Adding element to ArrayList
// by using standard add() method
// Custom input elements
arlst.add("A");
arlst.add("B");
arlst.add("C");
arlst.add("TajMahal");
// Printing the above elements inside ArrayList
System.out.println("List: " + arlst);
// Creating typesafe view of the specified list
// and applying checkedList
List tslst = Collections.checkedList(
arlst, String.class);
// Printing the updated elements of ArrayList
// after applying above operation
System.out.println("Typesafe view of List: "
+ tslst);
}
// Catch block to handle the exceptions
catch (IllegalArgumentException e) {
// Display message on console if exception
// occurs
System.out.println("Exception thrown : " + e);
}
}
}
Java
// Java program to Demonstrate checkedList() method
// of Collections class for a string value
// 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 an empty ArrayList of integer type
// by creating an object of List
List arlst = new ArrayList();
// Adding element to above ArrayList
// by using add() method
arlst.add(20);
arlst.add(30);
arlst.add(40);
arlst.add(50);
// Printing the elements of above ArrayList
System.out.println("List: " + arlst);
// Creating typesafe view of the specified list
// with usage of checkedList() method
List tslst = Collections.checkedList(
arlst, Integer.class);
// Printing the elements of ArrayList
// after performing above operation
System.out.println("Typesafe view of List: "
+ tslst);
}
// Catch block to handle the exceptions
catch (IllegalArgumentException e) {
// Display message if exception occurs
System.out.println("Exception thrown : " + e);
}
}
}
输出:
List: [A, B, C, TajMahal]
Typesafe view of List: [A, B, C, TajMahal]
示例 2:
Java
// Java program to Demonstrate checkedList() method
// of Collections class for a string value
// 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 an empty ArrayList of integer type
// by creating an object of List
List arlst = new ArrayList();
// Adding element to above ArrayList
// by using add() method
arlst.add(20);
arlst.add(30);
arlst.add(40);
arlst.add(50);
// Printing the elements of above ArrayList
System.out.println("List: " + arlst);
// Creating typesafe view of the specified list
// with usage of checkedList() method
List tslst = Collections.checkedList(
arlst, Integer.class);
// Printing the elements of ArrayList
// after performing above operation
System.out.println("Typesafe view of List: "
+ tslst);
}
// Catch block to handle the exceptions
catch (IllegalArgumentException e) {
// Display message if exception occurs
System.out.println("Exception thrown : " + e);
}
}
}
输出:
List: [20, 30, 40, 50]
Typesafe view of List: [20, 30, 40, 50]