import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class List3 {
public static void main(String[] args) {
//固定的List
var list1 = Arrays.asList(10,20,30,40);
System.out.println(list1.get(0));
//list1.remove(30);错误
var list2 = List.of(1,2,3);
//list2.clear(); 错误
//list2.add(5);
System.out.println(list2);
//动态List
var list3 = new ArrayList<Integer>();
var list4 = new ArrayList<String>(List.of("java","php","mysql"));
var list5 = new ArrayList<Integer>(Arrays.asList(10,20,30));
list5.add(40);
list4.add("python");
list3.addAll(List.of(11,22,33,44));
System.out.println(list3);
System.out.println(list4);
System.out.println(list5);
//数组转换List
Integer[] nums = new Integer[]{100,200,300};
// Object[] objects = Arrays.stream(nums).toArray();
var list6 = new ArrayList<>(Arrays.asList(nums));
list6.clear();
System.out.println(list6);
//List转换为Array数组
List<String> list7 = new ArrayList<>();
list7.addAll(List.of("java","springmvc","springboot"));
System.out.println(list7.size());
list7.remove("springmvc");
//Object[] ss = list7.toArray();
//System.out.println(Arrays.toString(ss));
String[] ss = new String[list7.size()];
list7.toArray(ss);
System.out.println(list7);
System.out.println(Arrays.toString(ss));
}
}
List集合、Arrays.asList()固定的List、动态List、数组转换List、List转换为Array数组
最新推荐文章于 2023-02-19 22:26:51 发布