package dataproject;
import java.util.Arrays;
import java.util.List;
public class TestArray {
public static void main(String[] args) {
//String[]数组转String类型的集合,当需要使用int,double等集合的时候,需要使用对应的对象
//如:数组int[]用Integer[],double[]用Double[]
// 因为List集合是对象的集合,而int、double等不是对象,所以需要用字段的对应对象类
//int数组转List
Integer [] myint = new Integer[]{1,2,3,4};
List<Integer> myList = Arrays.asList(myint);
for(Integer l:myList){
System.out.println(l);
}
//List转int数组
Integer[] otherint = myList.toArray(new Integer[0]);
for(Integer in:otherint){
System.out.println(in);
}
// //String数组转list
// String [] array = new String[]{"1","2","3","4"};
// List<String> list = Arrays.asList(array);
// System.out.println(list);
//
// //List转String数组
//java中的强制类型转换是针对单个对象才有效果的,而List是多对象的集合,所以将整个List强制转换是不行的
// String [] array1 = list.toArray(new String[0]);
// for(String s:array1){
// System.out.println(s);
// }
}
}
List和数组的转换
最新推荐文章于 2024-07-30 18:30:21 发布