之前笔试的时候,输出结果个数不确定,果断选择了用List<Integer> list = new ArrayList<>();存储,最后要求返回int[],直接调用了list.toArray(),报错了,该函数返回 Object[],果断加上参数list.toArray(new Integer[list.size()]),结果返回Integer[],最后时间来不及了,也没做出来,下来查资料写下这篇文章铭记。
数组类型转换
测试数据
//【字符串】切成【数组】
String[] arr = "1,2,3,4,5,6".split(",");
字符串数组 to (Integer、Long、Double)列表
传统方式
1.【String数组】转成【Long数组】 这里使用到了 Apache Commons BeanUtils 不过多介绍
//【String数组】转成【Long数组】
Long[] LongIds= (Long[]) ConvertUtils.convert(arr, Long.class);
2.【Long数组】 转 【ArrayList】
// Arrays.asList 返回固定大小的列表。
List<Long> list = Arrays.asList(LongIds);
// new ArrayList 创建动态的 ArrayList
ArrayList<Long> ids = new ArrayList<Long>(list);
Java8 Stream方式
List<Integer> intList = Arrays.stream(arr).map(Integer::valueOf).collect(toList());
System.out.println(intList);
List<Long> longList = Arrays.stream(arr).map(Long::valueOf).collect(toList());
System.out.println(longList);
List<Double> doubleList = Arrays.stream(arr).map(Double::valueOf).collect(toList());
System.out.println(doubleList);
数组 to 数组
字符串数组 to (int、long、double )数组
int[] ints = Arrays.stream(arr).mapToInt(Integer::valueOf).toArray();
long[] longs = Arrays.stream(arr).mapToLong(Long::valueOf).toArray();
double[] doubles = Arrays.stream(arr).mapToDouble(Double::valueOf).toArray();
int[]、long[]、double[] 数组互相转换
// int[] 2 long[]
long[] longs = Arrays.stream(ints).mapToLong(Long::valueOf).toArray();
// int[] 2 double[]
double[] doubles = Arrays.stream(ints).mapToDouble(Double::valueOf).toArray();
// long[] 2 int[]
int[] ints1 = Arrays.stream(longs).mapToInt(i -> (int)i).toArray();
// long[] 2 double[]
double[] doubles1 = Arrays.stream(longs).mapToDouble(Double::valueOf).toArray();
// double[] 2 int[]
int[] ints2 = Arrays.stream(doubles).mapToInt(i -> (int)i).toArray();
// double[] 2 long[]
long[] longs2 = Arrays.stream(doubles).mapToLong(i -> (long)i).toArray();
int[]、long[]、double[] 数组再转回 String[]
String[] strArr1 = Arrays.stream(ints).mapToObj(String::valueOf).toArray(String[]::new);
String[] strArr2 = Arrays.stream(longs).mapToObj(String::valueOf).toArray(String[]::new);
String[] strArr3 = Arrays.stream(doubles).mapToObj(String::valueOf).toArray(String[]::new);
基础类型数组 to 列表
Java8 以前用乖乖 for 循环
int[] intArr = {1, 2, 3, 4, 5, 6};
List ints = new ArrayList<>();
for (int i : intArr) {
ints.add(i);
}
log.info("{}", ints); // [1, 2, 3, 4, 5, 6]
Java8 以后用 stream + boxed
int数组 to 列表
int[] intArr = {1, 2, 3, 4, 5, 6};
List<Integer> intList = Arrays.stream(intArr).boxed().collect(Collectors.toList());
System.out.println(intList); // [1, 2, 3, 4, 5, 6]
long数组 to 列表
long[] longArr = {1L, 2L, 3L, 4L, 5L, 6L};
List<Long> longList = Arrays.stream(longArr).boxed().collect(Collectors.toList());
System.out.println(longList); // [1, 2, 3, 4, 5, 6]
double数组 to 列表
double[] doubleArr = {1d, 2d, 3d, 4d, 5d, 6d};
List<Double> doubleList = Arrays.stream(doubleArr).boxed().collect(Collectors.toList());
System.out.println(doubleList); // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
String数组 to 列表
String[] arr = "1,2,3,4,5,6".split(",");
List<String> strList = Arrays.stream(arr).collect(Collectors.toList());
System.out.println(strList ); // [1, 2, 3, 4, 5, 6]
String[] arr = "1,2,3,4,5,6".split(",");
// list 不可编辑。
List<String> list = Arrays.asList(arr);
System.out.println(list); // [1, 2, 3, 4, 5, 6]
// 下面会报错,因为 asList 返回的是 Arrays 内部类 ArrayList 它没实现 add
// ArrayList<String> list = Arrays.asList(arr);
// arrayList 不可编辑
ArrayList<String> arrayList = new ArrayList<String>(list);
System.out.println(arrayList); // [1, 2, 3, 4, 5, 6]
// Collections.addAll 一切正常
List<String> ints = new ArrayList<>();
Collections.addAll(ints, arr);
log.info("{}", ints); // [1, 2, 3, 4, 5, 6]
列表 to 数组
List to Object[]
Object[] objects = new ArrayList<Integer>(list).toArray();
System.out.println(Arrays.toString(objects)); // [1, 2, 3, 4, 5]
List to Integer[]
Integer[] integers = new ArrayList<Integer>(list).toArray(new Integer[0]);
System.out.println(Arrays.toString(integers)); // [1, 2, 3, 4, 5]
List to int[]
List<Integer> list = new ArrayList<>();
int[] ints = list.stream().mapToInt(Integer::intValue).toArray();
数组转 String
Arrays.toString
注意两边有中括号[]
//======================= Array ==============================
String[] arr = {"你好", "哈喽", "啦啦啦"};
String str1 = Arrays.toString(arr);
System.out.println(str1); // [你好, 哈喽, 啦啦啦]
String.join
String[] arr = {"你好", "哈喽", "啦啦啦"};
//======================= Array ==============================
String str3 = String.join(", ", arr);
System.out.println(str3); // 你好, 哈喽, 啦啦啦
//======================= List<T> ==============================
List<String> list = Arrays.asList(arr);
String string2 = String.join(" - ", list);
System.out.println(string2); // 你好 - 哈喽 - 啦啦啦
Collectors.joining
//======================= List<T> ==============================
String[] arr = {"你好", "哈喽", "啦啦啦"};
List<String> list = Arrays.asList(arr);
String string1 = list.stream().collect(Collectors.joining("; "));
System.out.println(string1); // 你好; 哈喽; 啦啦啦