当前位置: 代码网 > it编程>编程语言>Java > java中List的toArray()方法用法举例

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

2024年11月13日 Java 我要评论
toarray()介绍toarray()方法是list接口中提供的方法,用来实现list对象转换为数组对象的功能。toarray()方法有两种形式,无参方法和带泛型的方法,接下来给出例子。1.toar

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;
    }

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

		list<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.toarray(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();      
         
        // 思路同上。先将integer[]转成stream<integer>,再转成intstream。       
        // integer[] 转 list<integer>       
        list<integer> list2 = arrays.aslist(integers1);       
         
        // 最简单的方式。string[]转list<string>也同理。        
        // 同理       <br>         string[] strings1 = {"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()方法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com