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