List与数组 之间的转换

List与数组 之间的转换

package com.jo;

import java.util.List;
import java.util.ArrayList;

public class List2Array {

public static void main(String[] args) {
 // TODO Auto-generated method stub
 String[] ss ={"hello","world","huan","ying"};
 System.out.println("list2Array:"+ss.length);

 List ll = array2List(ss);
 System.out.println("array2List:"+ll);

 String[] ss2= list2Array(ll);
 System.out.println("list2Array:"+ss2.length);
}

//数组转换为List
public static List<String> array2List(String[] strs){
 List<String> list = new ArrayList<String>();
 for(String str:strs){
  list.add(str);
 }
 return list;
}
   //List转换为数组
public static String[] list2Array(List<String> list){
//  String[] strs = (String[])list.toArray();
 String[] strs1 = list.toArray(new String[list.size()]);
 return strs1;
}
}


你可能感兴趣的:(import,package,public)