【Java List与对象】List<T>对象和对象List<T>的区别(125)
短信预约 -IT技能 免费直播动态提醒
List<T>对象:对象里面的元素属性包含List<T>(只有一个对象);
对象List<T>:List<T>里面的数据是对象类型(可以有多组对象,但必须是同一个类型);
测试案例:
Book类:
import java.util.List;public class Book {private List name;private List price;public List getName() {return name;}public void setName(List name) {this.name = name;}public List getPrice() {return price;}public void setPrice(List price) {this.price = price;}public Book(List name, List price) {super();this.name = name;this.price = price;}public Book() {super();}}
Student类:
public class Student {private String name;private int age;private String address;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public Student(String name, int age, String address) {super();this.name = name;this.age = age;this.address = address;}public Student() {super();}}
测试Demo : (重点)
import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class day19 {public static void main(String[] args) {// List<T>对象:对象里面的元素属性包含List<T>(只有一个对象);List list = new ArrayList<>();Book b = new Book();List nameList = Arrays.asList("语文","数学","英语");List priceList = Arrays.asList(29,39,49);b.setName(nameList);b.setPrice(priceList);List nameList2 = b.getName();for(String s:nameList2) {System.out.println("List<T>对象name:"+s);}List priceList2 = b.getPrice();for(Integer s:priceList2) {System.out.println("List<T>对象price:"+s);}System.out.println("-------------------");// 对象List<T>:List<T>里面的数据是对象类型(可以有多组对象,但必须是同一个类型);List list2 = new ArrayList<>();list2.add(new Student("小婷",22,"宝鸡"));list2.add(new Student("小霞",24,"西安"));list2.add(new Student("小桐",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:汉中
来源地址:https://blog.csdn.net/qq_42139049/article/details/132566747
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341