文章目录
- Java数组int[]
- 一、数据数组数组初始化
- 二、结构int[] / List 转换
- 2.1 String[] / List转换
- 2.2 int[] / List 转换
- 三、用法数组拷贝
- 四、数据数组完整代码
Java数组int[]
一、结构数组初始化
// 数组初始化 public static void initArray() { int[] arr0 = {1,用法 2, 3}; int[] arr1 = new int[]{}; int[] arr2 = new int[]{1, 2, 3}; int[] arr3 = new int[5]; }
二、int[] / List 转换
区分引用数据类型(String)数组、数据数组基本数据类型数组
2.1 String[] / List转换
1、结构Collections提供了addAll()方法构造List,用法可将Array转为List。数据数组
2、结构List提供了toArray()方法用于将List转为Array:
- Object[] toArray();
- T[] toArray(T[] a); // 一般采用此支持泛型的用法有参toArray方法
// String[]数组与List转换 public static void stringArrConvertList() { // 一、String[]数组转List String[] strArr = new String[3]; // 1、数据数组Arrays.asList()转换后不能支持增删改修改List Liststrlist1 = Arrays.asList(strArr); System.out.println(Arrays.toString(strlist1.toArray())); // [null,结构 null, null] // 2、new ArrayList支持增删改 ListstrList2 = new ArrayList(Arrays.asList(strArr)); strList2.add("f"); System.out.println(Arrays.toString(strList2.toArray())); // [null,用法 null, null, f] // 3、集合工具类最高效 ListstrList3 = new ArrayList<>(strArr.length); Collections.addAll(strList3, strArr); strList3.add(0, "f"); System.out.println(Arrays.toString(strList3.toArray())); // [f, null, null, null] // 二、List转String[]数组 strList3.add(0, "RR"); strList3.add(1, "EE"); strList3.add(2, "VV"); System.out.println(Arrays.toString(strList3.toArray())); // [RR, EE, VV, f, null, null, null] String[] strRet = strList3.toArray(new String[]{}); String[] strRet2 = strList3.toArray(new String[strlist1.size()]); // 推荐用法 Object[] objRet = strList3.toArray(); // list.toArray()返回Object[]对象 // String[] strErr = (String[])strList3.toArray(); // 禁止使用会报错 System.out.println(Arrays.toString(strRet));}
2.2 int[] / List 转换
1、int[]转List步骤:
- 将原数组转为顺序流 Arrays.stream();
- 将流的元素装箱到包装类 IntStream.boxed();
- 将元素添加到新的List中 Collectors.toList();
2、List转int[]步骤:
- 将List转为流 list.stream();
- 过滤空对象
- 映射到int
- 将元素添加到新Array中
// 由于装箱机制,基本数据类型数组与List转换需借助stream public static void intArrConvertList() { // 一、int[]数组转List int[] intArr = new int[]{1, 2, 3}; ListintList = Arrays.stream(intArr).boxed().collect(Collectors.toList()); // 二、List转int[]数组 intList.add(5); intList.add(6); System.out.println(Arrays.toString(intList.toArray())); // [1, 2, 3, 5, 6] Object[] objArr = intList.toArray(); // list.toArray()返回Object[] 对象 Integer[] integerArr = intList.toArray(new Integer[intList.size()]); // 返回Integer[] 对象 int[] intRet = intList.stream().mapToInt(Integer::valueOf).toArray(); // 推荐写法 // 存在null时 intList.add(null); intList.add(8); System.out.println(Arrays.toString(intList.toArray())); // [1, 2, 3, 5, 6, null, 8] // 过滤null对象 int[] intRet1 = intList.stream() .filter(Objects::nonNull) .mapToInt(Integer::valueOf) .toArray(); // 转为默认值 int[] intRet2 = intList.stream() .mapToInt(i ->(i == null ? 0 : 1)) .toArray(); }
三、数组拷贝
使用clone()方法时注意区分深拷贝与浅拷贝概念。
// 数组拷贝 public static void copyArray() { methFor(); // for循环 methClone(); // clone() methSysArraycopy(); // System.arraycopy() methArraysCopyOf(); // Arrays.copyOf()和Arrays.copyOfRange() } public static void methFor() { int[] nums = {2,5,1,3,4,7}; int len = nums.length; int[] ret = new int[len]; for (int i = 0; i < len; i++) { ret[i] = nums[i]; } // Arrays.toString()转为String类型 System.out.println(Arrays.toString(ret)); //[2, 5, 1, 3, 4, 7] } public static void methClone() { // 注意区分深拷贝与浅拷贝,此处只以基本数据类型一维数组为例 int[] nums = {0,1,2,3,4,5}; int[] ret = nums.clone(); System.out.println(Arrays.toString(ret)); // [0, 1, 2, 3, 4, 5] // 基本数据类型数组的clone是深拷贝,重新开辟内存空间存放新数据 nums[3] = 10; // 原数组修改不影响新数组 System.out.println(Arrays.toString(ret)); // [0, 1, 2, 3, 4, 5] } public static void methArraysCopyOf() { int[] nums = {1,2,3,4,5,6,7}; // 参数:原数组,新数组长度 int[] ret = Arrays.copyOf(nums, nums.length); System.out.println(Arrays.toString(ret)); // [1, 2, 3, 4, 5, 6, 7] // 长度<原数组长,截断 ret = Arrays.copyOf(nums, 3); System.out.println(Arrays.toString(ret)); // [1, 2, 3] // 长度>原数组长,补零 ret = Arrays.copyOf(nums, 8); System.out.println(Arrays.toString(ret)); // [1, 2, 3, 4, 5, 6, 7, 0] // 参数:原数组,[from, to) ret = Arrays.copyOfRange(nums, 2, 2); System.out.println(Arrays.toString(ret)); // [] // 长度>原数组长,补零 ret = Arrays.copyOfRange(nums, 3, 9); System.out.println(Arrays.toString(ret)); // [4, 5, 6, 7, 0, 0] } public static void methSysArraycopy() { int[] nums = {1,2,3,4,5}; // 目标数组必须已经存在,且不会被重构, 拷贝范围[from,to) int[] ret = new int[7]; // 参数:原数组,原数组起始位,目标数组起始位,拷贝长度 System.arraycopy(nums, 2, ret, 3, 2); // 默认补0 System.out.println(Arrays.toString(ret)); // [0, 0, 0, 3, 4, 0, 0] }
四、完整代码
import java.util.*;import java.util.stream.Collectors;public class ArrayNotes { public static void main() { initArray(); // 数组初始化 arrConvertList(); // 数组/List转换 copyArray(); // 数组拷贝 } // 数组初始化 public static void initArray() { int[] arr0 = {1, 2, 3}; int[] arr1 = new int[]{}; int[] arr2 = new int[]{1, 2, 3}; int[] arr3 = new int[5]; } // 数组转list public static void arrConvertList() { // String[]数组与List转换 stringArrConvertList(); // int[]数组与List转换 intArrConvertList(); } // String[]数组与List转换 public static void stringArrConvertList() { // 一、String[]数组转List String[] strArr = new String[3]; // 1、Arrays.asList()转换后不能支持增删改修改List Liststrlist1 = Arrays.asList(strArr); System.out.println(Arrays.toString(strlist1.toArray())); // [null, null, null] // 2、new ArrayList支持增删改 ListstrList2 = new ArrayList(Arrays.asList(strArr)); strList2.add("f"); System.out.println(Arrays.toString(strList2.toArray())); // [null, null, null, f] // 3、集合工具类最高效 ListstrList3 = new ArrayList<>(strArr.length); Collections.addAll(strList3, strArr); strList3.add(0, "f"); System.out.println(Arrays.toString(strList3.toArray())); // [f, null, null, null] // 二、List转String[]数组 strList3.add(0, "RR"); strList3.add(1, "EE"); strList3.add(2, "VV"); System.out.println(Arrays.toString(strList3.toArray())); // [RR, EE, VV, f, null, null, null] String[] strRet = strList3.toArray(new String[]{}); String[] strRet2 = strList3.toArray(new String[strlist1.size()]); // 推荐用法 Object[] objRet = strList3.toArray(); // list.toArray()返回Object[]对象 // String[] strErr = (String[])strList3.toArray(); // 禁止使用会报错 System.out.println(Arrays.toString(strRet));} // 由于装箱机制,基本数据类型数组与List转换需借助stream public static void intArrConvertList() { // 一、int[]数组转List int[] intArr = new int[]{1, 2, 3}; ListintList = Arrays.stream(intArr).boxed().collect(Collectors.toList()); // 二、List转int[]数组 intList.add(5); intList.add(6); System.out.println(Arrays.toString(intList.toArray())); // [1, 2, 3, 5, 6] Object[] objArr = intList.toArray(); // list.toArray()返回Object[] 对象 Integer[] integerArr = intList.toArray(new Integer[intList.size()]); // 返回Integer[] 对象 int[] intRet = intList.stream().mapToInt(Integer::valueOf).toArray(); // 推荐写法 // 存在null时 intList.add(null); intList.add(8); System.out.println(Arrays.toString(intList.toArray())); // [1, 2, 3, 5, 6, null, 8] // 过滤null对象 int[] intRet1 = intList.stream() .filter(Objects::nonNull) .mapToInt(Integer::valueOf) .toArray(); // 转为默认值 int[] intRet2 = intList.stream() .mapToInt(i ->(i == null ? 0 : 1)) .toArray(); } // 数组拷贝 public static void copyArray() { methFor(); // for循环 methClone(); // clone() methSysArraycopy(); // System.arraycopy() methArraysCopyOf(); // Arrays.copyOf()和Arrays.copyOfRange() } public static void methFor() { int[] nums = {2,5,1,3,4,7}; int len = nums.length; int[] ret = new int[len]; for (int i = 0; i < len; i++) { ret[i] = nums[i]; } // Arrays.toString()转为String类型 System.out.println(Arrays.toString(ret)); //[2, 5, 1, 3, 4, 7] } public static void methClone() { // 注意区分深拷贝与浅拷贝,此处只以基本数据类型一维数组为例 int[] nums = {0,1,2,3,4,5}; int[] ret = nums.clone(); System.out.println(Arrays.toString(ret)); // [0, 1, 2, 3, 4, 5] // 基本数据类型数组的clone是深拷贝,重新开辟内存空间存放新数据 nums[3] = 10; // 原数组修改不影响新数组 System.out.println(Arrays.toString(ret)); // [0, 1, 2, 3, 4, 5] } public static void methArraysCopyOf() { int[] nums = {1,2,3,4,5,6,7}; // 参数:原数组,新数组长度 int[] ret = Arrays.copyOf(nums, nums.length); System.out.println(Arrays.toString(ret)); // [1, 2, 3, 4, 5, 6, 7] // 长度<原数组长,截断 ret = Arrays.copyOf(nums, 3); System.out.println(Arrays.toString(ret)); // [1, 2, 3] // 长度>原数组长,补零 ret = Arrays.copyOf(nums, 8); System.out.println(Arrays.toString(ret)); // [1, 2, 3, 4, 5, 6, 7, 0] // 参数:原数组,[from, to) ret = Arrays.copyOfRange(nums, 2, 2); System.out.println(Arrays.toString(ret)); // [] // 长度>原数组长,补零 ret = Arrays.copyOfRange(nums, 3, 9); System.out.println(Arrays.toString(ret)); // [4, 5, 6, 7, 0, 0] } public static void methSysArraycopy() { int[] nums = {1,2,3,4,5}; // 目标数组必须已经存在,且不会被重构, 拷贝范围[from,to) int[] ret = new int[7]; // 参数:原数组,原数组起始位,目标数组起始位,拷贝长度 System.arraycopy(nums, 2, ret, 3, 2); // 默认补0 System.out.println(Arrays.toString(ret)); // [0, 0, 0, 3, 4, 0, 0] }}
参考
https://blog.csdn.net/x541211190/article/details/79597236
https://www.techiedelight.com/zh/convert-int-array-integer-array-java/
作者:综合