0


list =和addAll在List<实体类>数组的应用

实体类 A

publicclassA{privateString name;privateInteger age;publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicIntegergetAge(){return age;}publicvoidsetAge(Integer age){this.age = age;}}

用=会报错

publicstaticvoidmain(String[] args){List<A>[] lists=newArrayList[4];List<A> list=newArrayList<>();A a=newA();
        a.setName("小米");
        a.setAge(8);
        list.add(a);A a1=newA();
        a1.setName("小明");
        a1.setAge(8);
        list.add(a1);A a2=newA();
        a2.setName("小红");
        a2.setAge(8);
        list.add(a2);A a3=newA();
        a3.setName("小绿");
        a3.setAge(8);
        list.add(a3);for(int i =0; i < list.size(); i++){List<A> list1=list.subList(i,i+1);
            lists[i]=list1;}}

在这里插入图片描述
用 addAll就不报错

publicstaticvoidmain(String[] args){List<A>[] lists=newArrayList[4];List<A> list=newArrayList<>();A a=newA();
        a.setName("小米");
        a.setAge(8);
        list.add(a);A a1=newA();
        a1.setName("小明");
        a1.setAge(8);
        list.add(a1);A a2=newA();
        a2.setName("小红");
        a2.setAge(8);
        list.add(a2);A a3=newA();
        a3.setName("小绿");
        a3.setAge(8);
        list.add(a3);for(int i =0; i < list.size(); i++){List<A> alist=newArrayList<>();List<A> list1=list.subList(i,i+1);
            alist.addAll(list1);
            lists[i]=alist;}}
标签: list

本文转载自: https://blog.csdn.net/qq_38030465/article/details/143905266
版权归原作者 小手cool 所有, 如有侵权,请联系我们删除。

“list =和addAll在List<实体类>数组的应用”的评论:

还没有评论