集册 Java实例教程 将集合转换为数组

将集合转换为数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

497
将集合转换为数组

import java.util.LinkedList;

import java.util.List;/** 来 自 N o w  J a v a  . c o m**/

import java.util.Map;

import java.util.Set;


public class Main {

  public static void main(String[] argv) {

    List list = new LinkedList<>();

    // Create an array containing the elements in a list

    Object[] objectArray = list.toArray();

    MyClass[] array = (MyClass[]) list.toArray(new MyClass[list.size()]);


    Set set = null;

    // Create an array containing the elements in a set

    objectArray = set.toArray();

    array = (MyClass[]) set.toArray(new MyClass[set.size()]);


    Map map = null;

    // Create an array containing the keys in a map/*n o w j a   v  a . c o m - 时  代  Java 提 供*/

    objectArray = map.keySet().toArray();

    array = (MyClass[]) map.keySet().toArray(new MyClass[set.size()]);


    
展开阅读全文