List<T>对象:对象里面的元素属性包含List<T>(只有一个对象);
对象List<T>:List<T>里面的数据是对象类型(可以有多组对象,但必须是同一个类型);
测试案例:
Book类:
import java.util.List;public class Book {private List<String> name;private List<Integer> price;public List<String> getName() {return name;}public void setName(List<String> name) {this.name = name;}public List<Integer> getPrice() {return price;}public void setPrice(List<Integer> price) {this.price = price;}public Book(List<String> name, List<Integer> 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<String> list = new ArrayList<>();Book b = new Book();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 = 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:29
List<T>对象price:39
List<T>对象price:49
-------------------
对象List<T>
name:小婷--age:22--address:宝鸡
name:小霞--age:24--address:西安
name:小桐--age:26--address:汉中