1. 程式人生 > >數組和集合的互相轉換

數組和集合的互相轉換

java

package Collection;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ArrayToConnection {
public static void main(String[] args) {
String[] str = {"java", "php", "c++"};

    //數組轉集合
    List<String> list = Arrays.asList(str);
    Iterator<String> it = list.iterator();
    while (it.hasNext()){
       String value =  it.next();
       System.out.println(value);
    }

    //集合轉數組
    String[] str2 = (String[]) list.toArray();
    for (String s : str2) {
        System.out.println(s);
    }
}

}

數組和集合的互相轉換