当前位置: 代码迷 >> 综合 >> List集合、Arrays.asList()固定的List、动态List、数组转换List、List转换为Array数组
  详细解决方案

List集合、Arrays.asList()固定的List、动态List、数组转换List、List转换为Array数组

热度:59   发布时间:2023-12-03 02:09:19.0
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class List3 {public static void main(String[] args) {//固定的Listvar 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);//动态Listvar 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);//数组转换ListInteger[] 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));}
}

  相关解决方案