开发者

java中List的toArray()方法用法举例

目录
  • toArray()介绍
  • 1.toArray()
  • 2.toArray(T[] a)
  • List转换为int[]的三种方法
    • 法一:循环赋值
    • 法二:通过泛型实现转换
    • 法三:使用流
  • 总结

    toArray()介绍

    toArray()方法是List接口中提供的方法,用来实现List对象转换为数组对象的功能。

    toArray()方法有两种形式,无参方法和带泛型的方法,接下来给出例子。

    1.toArray()

    	// toArray()源码
    	public Object[] toArray() {
            return Arrays.copyOf(elementData, size);
        }
    

    该方法不能指定转换数组的类型,返回值只能是Object()数组,所以得到返回值后往往需要做类型转换,将Object[]转换为我们需要的类型。但是,往往在转换这一部会出问题,如下例:

    List<Integer> list = new ArrayList<>();
            list.add(1);
            list.add(2);
            list.add(3);
            Integer[] res = new Integer[list.size()];
            res = (Integer[])list.toArray();

    该代码能通过语法检查,但是在运行时会报类型转换错误,说明Object()不能简单地转换为其他类型的数组。

    Java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
    

    2.toArray(T[] a)

    	// toArray(T[] a)源码
    	public <T> T[] toArray(T[] a) {
            if (a.length < size)
                // Make a new array of a's runtime type, but my contents:
                return (T[]) Arrays.copyOf(elementData, size, a.getClass());
            System.arraycopy(elementData, 0, a, 0, size);
            if (a.length > size)
                a[size] = null;
            return a;
        }
    

    该方法比前一种无参的方法更灵活,要求用户提供一个目标对象的泛型,在数组转换后,会返回一个指定类型的数组,不存在类型转换错误。使用举例:

    		Liandroidst<String> list = new ArrayList<>();
            list.add("1");
            list.add("2");
            list.add("3");
            String[] res = new String[list.size()];
    

    这样就可以将List对象转换为String[]了。

    List转换为int[]的三种方法

    上边介绍toArray()时给出了两种方法,第二种带泛型的方法使用范围更广。我最近使用了第二个方法,发现了一些问题。我要实现的功能如下:

    给定 一个List对象:List list = new ArrayList<>();将其转换为一个int[]数组。

    我toArray()带泛型的方法来实现,代码如下:

    		List<Integer> list = new ArrayList<>();
            list.add(1);
            list.add(2);
            list.add(3);
            int[] res = new int[list.size()];
            res = list.toArray(res);
    

    但是实际上上面的代码是错的,因为toArray()的泛型参数不能是int,只能是其包装类Integer,所以通过toArray()不能直接将List对象转换为一般类型的数组,具体的转换方法有如下三种:

    法一:循环赋值

    将List中的元素一个个取出来,赋值到int[]数组中的对应位置。

    		List<Integer> list = new ArrayList<>();
            list.add(1);
            list.add(2);
            list.add(3);
            int[] res = new int[list.size()];
            for(int i = 0; i < list.size(); i++){
            	res[i] = list[i];
            }
    

    这个方法虽然需要遍历,但是思路简单,一般不会出错。

    法二:通过泛型实现转换

    以最终目标数组为int[]为例,从List转换为int[]数组,可以借助于Integer[]数组来实现,代码如下:

    		List<Integer> list = new ArrayList<>();
            list.add(1);
            list.add(2);
            list.add(3);
            Integer[] res = new Integer[list.size()];
            res = list.toArray(res);
    

    这样可以得到一个Integer[]数组,然后再使用循环赋值将Integer[]数组转换为int[]数组即可。

    法三:使用流

    使用Java1.8中提供的新方法来实现。以下是List、Integer[]、int[]三者的相互转换代码

    		int[] data = {4, 5, 3, 6, 2, 5, 1};        
            // int[] 转 List<Integer>       
            List<Integer> list1 = Arrays.stream(data).boxed().collect(Collectors.toList());       
            /编程/ Arrays.stream(arr) 可以替换成IntStream.of(arr)。      
            // 1.使用Arrays.stream将int[]转换成IntStream。       
            // 2.使用IntStream中的boxed()装箱。将IntStream转换成Stream<Integer>。       
            // 3.使用Stream的collect(),将Stream<T>转换成List<T>,因此正是List<Integer>。        
             
            // int[] 转 Integer[]       
            Integer[] integers1 = Arrays.stream(data).boxed().toArray(Integer[]::new);       
            // 前两步同上,此时是Stream<Integer>。      
            // 然后使用Stream的toArray,传入IntFunction<A[]> generator。       
            // 这样就可以返回Integer数组。      
            // 不然默认是Object[]。
             
            // List<Integer> 转 Integer[]       
            Integer[] integers2 = list1.toArraywww.devze.com(new Integer[0]);       
             
            //  调用toArray。传入参数T[] a。这种用法是目前推荐的。      
            // List<String>转String[]也同理。        
             
            // List<Integer> 转 int[]       
            int[] arr1 = list1.stream().mapToInt(Integer::valueOf).toArray();       
             
            // 想要转换成int[]类型,就得先转成IntStream。      
            // 这里就通过mapToInt()把Stream<Integer>调用Integer::valueOf来转成IntStream         
            // 而IntStream中默认toArray()转成int[]。       
             
            // Integer[] 转 int[]       
            int[] arr2 = Arrays.stream(integers1).mapToInt(Integer::valueOf).toArray();      
             
           www.devze.com // 思路同上。先将Integer[]转成Stream<Integer>,再转成IntStream。       
            // Integer[] 转 List<Integer>       
            List<Integer> list2 = Arrays.asList(integers1);       
             
            // 最简单的方式。String[]转List<String>也同理。        
            // 同理       <br>         String[] pythonstrings1 = {"a", "b", "c"};      
             
            // String[] 转 List<String>       
            List<String> list3 = Arrays.asList(strings1);       
             
            // List<String> 转 String[]       
            String[] strings2 = list3.toArray(new String[0]);   
    

    总结

    到此这篇关于java中List的toArray()方法用法的文章就介绍到这了,更多相关java List的toArray()方法内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜