当前位置: 首页 > news >正文

建各公司网站要多少钱企业网站推广方案的策划

建各公司网站要多少钱,企业网站推广方案的策划,网上怎样查询企业资质,如何进行免费网络推广文章目录 List -> Array1. 调用toArray()方法直接返回一个Object[]数组&#xff1a;2. 给toArray(T[])传入一个类型相同的Array&#xff0c;List内部自动把元素复制到传入的Array中&#xff1a;3. 通过List接口定义的T[] toArray(IntFunction<T[]> generator)方法&…

文章目录

  • List -> Array
    • 1. 调用toArray()方法直接返回一个Object[]数组:
    • 2. 给toArray(T[])传入一个类型相同的Array,List内部自动把元素复制到传入的Array中:
    • 3. 通过List接口定义的T[] toArray(IntFunction<T[]> generator)方法:
  • Array-> List

List -> Array

1. 调用toArray()方法直接返回一个Object[]数组:

import java.util.List;
public class Main {public static void main(String[] args) {List<String> list = List.of("apple", "pear", "banana");Object[] array = list.toArray();for (Object s : array) {System.out.println(s);}}
}

这种方法会丢失类型信息,所以实际应用很少。

2. 给toArray(T[])传入一个类型相同的Array,List内部自动把元素复制到传入的Array中:

import java.util.List;
public class Main {public static void main(String[] args) {List<Integer> list = List.of(12, 34, 56);Integer[] array = list.toArray(new Integer[3]);for (Integer n : array) {System.out.println(n);}}
}

注意到这个toArray(T[])方法的泛型参数并不是List接口定义的泛型参数,所以,我们实际上可以传入其他类型的数组,例如我们传入Number类型的数组,返回的仍然是Number类型:

import java.util.List;
public class Main {public static void main(String[] args) {List<Integer> list = List.of(12, 34, 56);Number[] array = list.toArray(new Number[3]);for (Number n : array) {System.out.println(n);}}
}

但是,如果我们传入类型不匹配的数组,例如,String[]类型的数组,由于List的元素是Integer,所以无法放入String数组,这个方法会抛出ArrayStoreException。

如果我们传入的数组大小和List实际的元素个数不一致怎么办?根据List接口的文档,我们可以知道:

如果传入的数组不够大,那么List内部会创建一个新的刚好够大的数组,填充后返回;如果传入的数组比List元素还要多,那么填充完元素后,剩下的数组元素一律填充null。

实际上,最常用的是传入一个“恰好”大小的数组:

Integer[] array = list.toArray(new Integer[list.size()]);

3. 通过List接口定义的T[] toArray(IntFunction<T[]> generator)方法:

Integer[] array = list.toArray(Integer[]::new);

这种函数式写法我们会在后续讲到。

Array-> List

通过List.of(T…)方法最简单:

Integer[] array = { 1, 2, 3 };
List<Integer> list = List.of(array);

对于JDK 11之前的版本,可以使用Arrays.asList(T…)方法把数组转换成List。

要注意的是,返回的List不一定就是ArrayList或者LinkedList,因为List只是一个接口,如果我们调用List.of(),它返回的是一个只读List:

import java.util.List;
public class Main {public static void main(String[] args) {List<Integer> list = List.of(12, 34, 56);list.add(999); // UnsupportedOperationException}
}

对只读List调用add()、remove()方法会抛出UnsupportedOperationException。


文章转载自:

http://l2yXw34Y.pndw.cn
http://f0fOEZa4.pndw.cn
http://ELXK8o64.pndw.cn
http://TxY8qqgL.pndw.cn
http://cvXJclf0.pndw.cn
http://s0g6aQrY.pndw.cn
http://A8k03TKr.pndw.cn
http://jd2iir1L.pndw.cn
http://FcYKxd5Z.pndw.cn
http://Iof3c6RT.pndw.cn
http://Df95s2QF.pndw.cn
http://96IO3kFI.pndw.cn
http://BvzrlEzQ.pndw.cn
http://aS32vj8d.pndw.cn
http://3EsRJR14.pndw.cn
http://y57YHrBb.pndw.cn
http://ByGAz88J.pndw.cn
http://xIy349ZL.pndw.cn
http://aTbb9Cw0.pndw.cn
http://I97LXhmC.pndw.cn
http://Y2cbDvTu.pndw.cn
http://kOo9JZf3.pndw.cn
http://omtCpPOX.pndw.cn
http://oD7PfLL9.pndw.cn
http://lIbZhOOV.pndw.cn
http://X3ZAQHRc.pndw.cn
http://hpyeElka.pndw.cn
http://SjQzyp2d.pndw.cn
http://n1Z5SwFw.pndw.cn
http://VZx5Tzn1.pndw.cn
http://www.viphn.cn/news/120/

相关文章: