在Java编程中,我们常常需要将数组转换成List。这种操作非常常见,本文将介绍多种方法用于将数组转换为List。
一、如何将数组转为对象
方法1:利用Arrays.asList()方法。
String[] strArray = { "a", "b", "c"}; Listlist = Arrays.asList(strArray);
方法2:利用Collections.addAll()方法
Integer[] intArray = { 1, 2, 3 }; Listlist = new ArrayList (); Collections.addAll(list, intArray);
二、如何将数组转为字符串
方法1:利用Arrays.toString()方法
String[] strArray = { "a", "b", "c" }; String str = Arrays.toString(strArray);
方法2:遍历数组并将元素拼接成字符串
String[] strArray = { "a", "b", "c" }; StringBuilder sb = new StringBuilder(); for (String str : strArray) { sb.append(str); } String str = sb.toString();
三、如何将数组转为集合
方法1:利用Arrays.asList()方法
Integer[] intArray = { 1, 2, 3 }; Listlist = Arrays.asList(intArray);
方法2:利用Collections.addAll()方法
Integer[] intArray = { 1, 2, 3 }; Listlist = new ArrayList (); Collections.addAll(list, intArray);
四、如何将int数组转为集合
方法1:利用Java8的Stream API
int[] intArray = { 1, 2, 3 }; Listlist = IntStream.of(intArray).boxed().collect(Collectors.toList());
方法2:利用Apache Commons Lang3包中的ArrayUtils.toObject()方法
int[] intArray = { 1, 2, 3 }; Listlist = Arrays.asList(ArrayUtils.toObject(intArray));
五、如何将数组转为树形结构
方法1:利用递归
class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public TreeNode arrayToTree(int[] arr, int start, int end) { if (start > end) { return null; } int mid = (start + end) / 2; TreeNode root = new TreeNode(arr[mid]); root.left = arrayToTree(arr, start, mid - 1); root.right = arrayToTree(arr, mid + 1, end); return root; }
方法2:利用队列
class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public TreeNode arrayToTree(int[] arr) { if (arr == null || arr.length == 0) { return null; } int i = 0; TreeNode root = new TreeNode(arr[i]); Queuequeue = new LinkedList (); queue.offer(root); while (!queue.isEmpty()) { TreeNode node = queue.poll(); if (++i < arr.length) { node.left = new TreeNode(arr[i]); queue.offer(node.left); } if (++i < arr.length) { node.right = new TreeNode(arr[i]); queue.offer(node.right); } } return root; }
六、对象转为数组
方法1:利用反射
public Object[] toArray(Object obj) throws IllegalAccessException { Class clazz = obj.getClass(); Field[] fields = clazz.getDeclaredFields(); Object[] result = new Object[fields.length]; for (int i = 0; i < fields.length; i++) { fields[i].setAccessible(true); result[i] = fields[i].get(obj); } return result; }
方法2:利用Java8的Stream API
public Object[] toArray(Object obj) throws IllegalAccessException { Class clazz = obj.getClass(); Field[] fields = clazz.getDeclaredFields(); Object[] result = Arrays.stream(fields) .map(f -> { f.setAccessible(true); try { return f.get(obj); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }).toArray(); return result; }
七、arraylist转为数组
方法1:利用toArray()方法
Listlist = new ArrayList (); Integer[] arr = list.toArray(new Integer[0]);
方法2:利用Java8的Stream API
Listlist = new ArrayList (); Integer[] arr = list.stream().toArray(Integer[]::new);
八、怎么把数组转为字符串
方法1:利用Arrays.toString()方法
String[] strArray = { "a", "b", "c" }; String str = Arrays.toString(strArray);
方法2:遍历数组并将元素拼接成字符串
String[] strArray = { "a", "b", "c" }; StringBuilder sb = new StringBuilder(); for (String str : strArray) { sb.append(str); } String str = sb.toString();
九、数组如何转为集合
方法1:利用Arrays.asList()方法
String[] strArray = { "a", "b", "c" }; Listlist = Arrays.asList(strArray);
方法2:利用Collections.addAll()方法
String[] strArray = { "a", "b", "c" }; Listlist = new ArrayList (); Collections.addAll(list, strArray);
本文介绍了多种方法用于将数组转换为List,每种方法都有利弊。选择应该根据具体情况而定,建议多加尝试。