Java中数组,集合的相互转换:
Set转化成List:
Set<Character> nums=new Hashset<Character>;
List<Character> list=new ArrayList<Character>(nums);
List转化为Set:
Set<Integer> nums=new HashSet<Integer>(list);
数组转化为List:
List<Integer> list=Array.asList(nums);//此方法转换出的list不可进行增删等操作
List<Integer> arrayList=new ArrayList<Integer>(list);//此次转化的arrayList可以进行增删操作
List转化为数组:
List<Integer> list=new ArrayList<Integer>();
int[] array=new int[];
array=list.toArray(array);//注意要使用toArray的带参方法,否则返回数组为Object[]类型
Set转化为数组:
同上,使用toArray方法;
数组转化为Set:
先转List,然后再转Set
int[] nums=[1,2,3,4,5];
List<Integer> list=Array.asList(nums);
Set<Integer> set=new HashSet<Integer>(list);