| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package course;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo01
- * @description List.of方法
- * @create 2026/7/30
- */
- public class Demo01 {
- /*
- List接口的特殊方法
- List.of()
- ....... 系列方法
- */
- public static void main(String[] args) {
- //
- List<String> list1=new ArrayList<>();
- //JAVA9之后,为List提供了一系列的of方法,用来快速创建一个List列表集合对象,而且是不可改变的集合。
- List<String> list = List.of("张三","李四","wangwu");
- //list.add("张三");
- //list.set(1,"晚五");
- for (int i = 0; i < list.size(); i++) {
- System.out.println(list.get(i));
- }
- Student s1=new Student("张三",20,"S001");
- Student s2=new Student("张三2",20,"S002");
- Student s3=new Student("张三3",20,"S003");
- Student s4=new Student("张三4",20,"S004");
- List<Student> studentList = List.of(s1, s2, s3, s4);
- for (int i = 0; i < studentList.size(); i++) {
- System.out.println(studentList.get(i));
- }
- s2.setName("李四");
- for (int i = 0; i < studentList.size(); i++) {
- System.out.println(studentList.get(i));
- }
- studentList=List.of();
- }
- }
|