[英]Arraylist to array copy Java
ArrayList<ArrayList<Integer>> L1=new ArrayList<>();
ArrayList<Integer> L2 = new ArrayList<>();
ArrayList<Integer> L3 = new ArrayList<>();
ArrayList<Integer> L4 = new ArrayList<>();
L2.add(5); L3.add(6); L4.add(9);
L2.add(2); L3.add(1); L4.add(3);
L2.add(1); L3.add(1); L4.add(2);
L1.add(L2); L1.add(L3); L1.add(L4);
L1.remove(L1.size() - 1);
Now I want to save the element removed from L1 in an array.现在我想将从 L1 中删除的元素保存在一个数组中。 As this list contains three elements, I want to create an array like int ar[] = new int[3];
由于此列表包含三个元素,我想创建一个数组,如int ar[] = new int[3];
and save this removed value into this array.并将这个删除的值保存到这个数组中。
If you're ok with Integer[]
array, then just use toArray
method:如果您对Integer[]
数组没问题,那么只需使用toArray
方法:
ArrayList<Integer> removed = L1.remove(L1.size() - 1);
Integer[] integers = removed.toArray(new Integer[removed.size()]);
If you want int[]
array, you could use streams:如果你想要int[]
数组,你可以使用流:
int[] ints = L1.remove(L1.size() - 1).stream().mapToInt(i -> i).toArray();
.mapToInt(i -> i)
is used to unbox Integer
values to int
.mapToInt(i -> i)
用于将Integer
数值拆箱为int
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.