0


【Java List与对象】List<T>对象和对象List<T>的区别(125)

List<T>对象:对象里面的元素属性包含List<T>(只有一个对象);
对象List<T>:List<T>里面的数据是对象类型(可以有多组对象,但必须是同一个类型);

测试案例:

Book类:

importjava.util.List;publicclassBook{privateList<String> name;privateList<Integer> price;publicList<String>getName(){return name;}publicvoidsetName(List<String> name){this.name = name;}publicList<Integer>getPrice(){return price;}publicvoidsetPrice(List<Integer> price){this.price = price;}publicBook(List<String> name,List<Integer> price){super();this.name = name;this.price = price;}publicBook(){super();}}

Student类:

publicclassStudent{privateString name;privateint age;privateString address;publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicintgetAge(){return age;}publicvoidsetAge(int age){this.age = age;}publicStringgetAddress(){return address;}publicvoidsetAddress(String address){this.address = address;}publicStudent(String name,int age,String address){super();this.name = name;this.age = age;this.address = address;}publicStudent(){super();}}

测试Demo : (重点)

importjava.util.ArrayList;importjava.util.Arrays;importjava.util.List;publicclass day19 {publicstaticvoidmain(String[] args){// List<T>对象:对象里面的元素属性包含List<T>(只有一个对象);List<String> list =newArrayList<>();Book b =newBook();List<String> nameList =Arrays.asList("语文","数学","英语");List<Integer> priceList =Arrays.asList(29,39,49);
        b.setName(nameList);
        b.setPrice(priceList);List<String> nameList2 = b.getName();for(String s:nameList2){System.out.println("List<T>对象name:"+s);}List<Integer> priceList2 = b.getPrice();for(Integer s:priceList2){System.out.println("List<T>对象price:"+s);}System.out.println("-------------------");// 对象List<T>:List<T>里面的数据是对象类型(可以有多组对象,但必须是同一个类型);List<Student> list2 =newArrayList<>();
        list2.add(newStudent("小婷",22,"宝鸡"));
        list2.add(newStudent("小霞",24,"西安"));
        list2.add(newStudent("小桐",26,"汉中"));System.out.println("对象List<T>");for(Student s : list2){Student st = s;System.out.println("name:"+st.getName()+"--"+"age:"+st.getAge()+"--"+"address:"+st.getAddress());}}}

测试输出

List<T>对象name:语文
List<T>对象name:数学
List<T>对象name:英语
List<T>对象price:29List<T>对象price:39List<T>对象price:49-------------------
对象List<T>
name:小婷--age:22--address:宝鸡
name:小霞--age:24--address:西安
name:小桐--age:26--address:汉中
标签: java list windows

本文转载自: https://blog.csdn.net/qq_42139049/article/details/132566747
版权归原作者 DevínKelly 所有, 如有侵权,请联系我们删除。

“【Java List与对象】List<T>对象和对象List<T>的区别(125)”的评论:

还没有评论