Java 集合 list() 方法



描述

Java Collections list(Enumeration<T>) 方法用於獲取一個數組列表,該列表包含指定列舉返回的元素,元素順序與列舉返回的順序相同。

宣告

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

public static <T> ArrayList<T> list(Enumeration<T> e)

引數

e - 這是為返回的陣列列表提供元素的列舉。

返回值

方法呼叫返回指定目標列表在指定源列表中最後一次出現的起始位置,如果不存在這樣的出現,則返回 -1。

異常

從整數向量獲取列表示例

以下示例演示了 Java Collection list(Enumeration) 方法的用法。我們建立了一個向量物件,並用一些整數填充它。然後檢索其列舉,並使用 list(Enumeration) 方法準備並列印一個數組列表。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create vector and array list
      List<Integer> arrlist = new ArrayList<>();
      Vector<Integer> v = new Vector<>();

      // populate the vector
      v.add(1);       
      v.add(2);
      v.add(3);
      v.add(4);
      v.add(5);

      // create enumeration
      Enumeration<Integer> e = v.elements();

      // get the list
      arrlist = Collections.list(e);

      System.out.println("Value of returned list: "+arrlist);
   }    
}  

輸出

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

Value of returned list: [1, 2, 3, 4, 5]

從字串向量獲取列表示例

以下示例演示了 Java Collection list(Enumeration) 方法的用法。我們建立了一個向量物件,並用一些字串填充它。然後檢索其列舉,並使用 list(Enumeration) 方法準備並列印一個數組列表。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create vector and array list
      List<String> arrlist = new ArrayList<>();
      Vector<String> v = new Vector<>();

      // populate the vector
      v.add("A");       
      v.add("B");
      v.add("C");
      v.add("D");
      v.add("E");

      // create enumeration
      Enumeration<String> e = v.elements();

      // get the list
      arrlist = Collections.list(e);

      System.out.println("Value of returned list: "+arrlist);
   }    
}  

輸出

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

Value of returned list: [A, B, C, D, E]

從物件向量獲取列表示例

以下示例演示了 Java Collection list(Enumeration) 方法的用法。我們建立了一個向量物件,並用一些 Student 物件填充它。然後檢索其列舉,並使用 list(Enumeration) 方法準備並列印一個數組列表。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

public class CollectionsDemo {
   public static void main(String args[]) {
      
      // create vector and array list
      List<Student> arrlist = new ArrayList<>();
      Vector<Student> v = new Vector<>();

      // populate the vector
      v.add(new Student(1, "Julie"));
      v.add(new Student(2, "Robert"));
      v.add(new Student(3, "Adam"));
      v.add(new Student(4, "Jene"));
      v.add(new Student(5, "Joe"));

      // create enumeration
      Enumeration<Student> e = v.elements();

      // get the list
      arrlist = Collections.list(e);

      System.out.println("Value of returned list: "+arrlist);
   }    
}
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 + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

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

Value of returned list: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 4, Jene ], [ 5, Joe ]]
java_util_collections.htm
廣告