Java 集合 checkedList() 方法



描述

Java 集合 checkedList(List<E>, Class<E>) 方法用於獲取指定列表的動態型別安全檢視。

宣告

以下是 java.util.Collections.checkedList() 方法的宣告。

public static <E> List<E> checkedList(List<E> list,Class<E> type)

引數

  • list − 這是要返回其動態型別安全檢視的列表。

  • type − 這是列表允許儲存的元素型別。

返回值

方法呼叫返回指定列表的動態型別安全檢視。

異常

從整數集合獲取型別安全列表示例

以下示例演示瞭如何使用 Java 集合 checkedList(List,Class) 方法來獲取整數列表的型別安全檢視。我們建立了一個包含一些整數的 List 物件,列印了原始列表。使用 checkedList(List, Integer) 方法,我們獲取了一個 Integer 列表,然後列印它。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5));

      System.out.println("Initial collection value: " + list);

      List<Integer> safeList = Collections.checkedList(list, Integer.class);
      System.out.println("Typesafe View: "+safeList);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Initial collection value: [1, 2, 3, 4, 5]
Typesafe View: [1, 2, 3, 4, 5]

從字串集合獲取型別安全列表示例

以下示例演示瞭如何使用 Java 集合 checkedList(List,Class) 方法來獲取字串列表的型別安全檢視。我們建立了一個包含一些字串的 List 物件,列印了原始列表。使用 checkedList(List, String) 方法,我們獲取了一個 String 列表,然後列印它。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<String> list = new ArrayList<>(Arrays.asList("Welcome","to","Tutorialspoint"));

      System.out.println("Initial collection value: " + list);

      List<String> safeList = Collections.checkedList(list, String.class);
      System.out.println("Typesafe View: "+safeList);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Initial collection value: [Welcome, to, Tutorialspoint]
Typesafe View: [Welcome, to, Tutorialspoint]

從物件集合獲取型別安全列表示例

以下示例演示瞭如何使用 Java 集合 checkedList(List,Class) 方法來獲取 Student 物件列表的型別安全檢視。我們建立了一個包含一些 Student 物件的 List 物件,列印了原始列表。使用 checkedList(List, Student) 方法,我們獲取了一個 Student 列表,然後列印它。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam")));

      System.out.println("Initial collection value: " + list);

      List<Student> safeList = Collections.checkedList(list, Student.class);
      System.out.println("Typesafe View: "+safeList);
   }
}
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 + " ]";
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Initial collection value: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Typesafe View: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_collections.htm
廣告
© . All rights reserved.