方法1:Arrays.asList()
将一个数组转化为一个List对象.
例:
public class User {
private String name="";
// List属性
private List<Book> bookList = new ArrayList<>();
public User() {
}
public User(String name, List<Book> bookList) {
this.name = name;
this.bookList = bookList;
}
// 。。。。
public void printBooks(){
for(Book b : bookList)
System.out.println(b.getName());
}
public static void main(String[] args) {
Book book1 = new Book("Java");
Book book2 = new Book("C++");
// 不用再单独新建一个List作为参数传入了
User user = new User("lisi", Arrays.asList(book1,book2));
System.out.println(user.getName());
user.printBooks();
}
}