Demo01.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package course;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. /**
  5. * @author WanJl
  6. * @version 1.0
  7. * @title Demo01
  8. * @description List.of方法
  9. * @create 2026/7/30
  10. */
  11. public class Demo01 {
  12. /*
  13. List接口的特殊方法
  14. List.of()
  15. ....... 系列方法
  16. */
  17. public static void main(String[] args) {
  18. //
  19. List<String> list1=new ArrayList<>();
  20. //JAVA9之后,为List提供了一系列的of方法,用来快速创建一个List列表集合对象,而且是不可改变的集合。
  21. List<String> list = List.of("张三","李四","wangwu");
  22. //list.add("张三");
  23. //list.set(1,"晚五");
  24. for (int i = 0; i < list.size(); i++) {
  25. System.out.println(list.get(i));
  26. }
  27. Student s1=new Student("张三",20,"S001");
  28. Student s2=new Student("张三2",20,"S002");
  29. Student s3=new Student("张三3",20,"S003");
  30. Student s4=new Student("张三4",20,"S004");
  31. List<Student> studentList = List.of(s1, s2, s3, s4);
  32. for (int i = 0; i < studentList.size(); i++) {
  33. System.out.println(studentList.get(i));
  34. }
  35. s2.setName("李四");
  36. for (int i = 0; i < studentList.size(); i++) {
  37. System.out.println(studentList.get(i));
  38. }
  39. studentList=List.of();
  40. }
  41. }