数组==>集合
使用 Arrays 类提供的方法快速转换,Arrays.asList();
public class ArrayToList {
public static void main(String[] args) {
String[] arrs = new String[3];
arrs[0] ="111";
arrs[1] ="222";
arrs[2] ="333";
List<String> list = Arrays.asList(arrs);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
集合==>数组
ArrayList<Integer> temp = new ArrayList<>();
temp.add(0);
temp.add(1);
temp.add(2);
temp.add(3);
temp.add(4);
temp.add(5);
Integer[] res = temp.toArray(new Integer[]{});
System.out.print(Arrays.toString(res));
// Integer集合转Integer数组,注意Integer集合不能直接转为int数组,只能通过遍历转换。
int[] t = new int[res.length];
System.out.print(Arrays.toString(res));
for(int i=0;i<res.length-1;i++){
t[i] = res[i];
}
网友评论