package com.Mxhlin.arrayList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Mxhlin
* @Email [email protected]
* @Date 2022/09/19/21:01
* @Version
* @Description ArrayList和Array数组类型转换
*/
public class ArrayListOrArray {
public static void main(String[] args) {
Integer[] in = new Integer[]{10,20,30,40,50,60,70};
List<Integer> list = Arrays.asList(in);
List<Integer> list1 = List.of(1, 2, 3);
System.out.println(list.size());// 7
System.out.println(list1.size());// 3
// list.clear();// 异常
// list1.clear();// 异常
// 数组 -> List
ArrayList list3 = new ArrayList(list);
System.out.println(list3.size());// 7
list3.add(500);
list3.addAll(List.of(900,800,700));
System.out.println(list3.size());// 11
System.out.println(list3);// [10, 20, 30, 40, 50, 60, 70, 500, 900, 800, 700]
System.out.println(Arrays.toString(in));// [10, 20, 30, 40, 50, 60, 70]
// List -> 数组
Object[] array = list3.toArray();
System.out.println(array.length);// 11
// for (Object o : array) {
// System.out.println(o);
// }
// 数组 -> List
Integer[] integers = {100, 200, 300};
ArrayList<Integer> list4 = new ArrayList<>(Arrays.asList(integers));
System.out.println(list4.size());// 3
System.out.println(list4);// [100, 200, 300]
// List -> Array
List<String> list5 = new ArrayList<>();
list5.addAll(List.of("springmvc","java","springboot"));
System.out.println(list5.size());// 3
// 删除指定字符串
list5.remove("java");
System.out.println(list5.size());// 2
// Object[] objects = list5.toArray();
// String string = Arrays.toString(objects);
// System.out.println(string);
String[] ss = new String[list5.size()];
String[] strings = list5.toArray(ss);
for (String string : strings) {
System.out.println(string);// springmvc springboot
}
System.out.println(strings.length);// 2
}
}
标签:类型转换,ArrayList,List,System,println,Array,size,out
From: https://www.cnblogs.com/xhlin/p/16709141.html