Java Collections list()

list() 获取一个数组列表,该列表包含指定枚举返回的元素的顺序,这些元素按枚举返回的顺序排列。

1 语法

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

2 参数

e:返回的数组列表提供元素的枚举。

3 返回值

返回包含由指定的枚举返回的元素的数组列表。

4 Collections list()示例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.list的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //Create vector and array list
        List<String> arrlist = new ArrayList<String>();
        Vector v = new Vector();
        //Populate the vector
        v.add("A");
        v.add("B");
        v.add("C");
        v.add("D");
        v.add("E");
        //Create enumeration
        Enumeration 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]

5 Collections list()示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.list的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //Create vector and array list
        List<Integer> arrlist = new ArrayList<>();
        Vector v = new Vector();
        //Populate the vector
        v.add(1);
        v.add(2);
        v.add(3);
        v.add(4);
        v.add(5);
        //Create enumeration
        Enumeration en = v.elements();
        //Get the list
        arrlist = Collections.list(en);
        System.out.println("Value of returned list: "+arrlist);
    }
}

输出结果为:

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

6 Collections list()示例3

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.list的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        Vector<String> vt = new Vector<String>();
        vt.add("Java");
        vt.add("PHP");
        vt.add("Array");
        vt.add("String");
        vt.add("C++");
        Enumeration<String> enm = vt.elements();
        List<String> ll = Collections.list(enm);
        System.out.println("List elements: " + ll);
    }
}

输出结果为:

List elements: [Java, PHP, Array, String, C++]

 

热门文章

优秀文章