1.情景展示
java基本數據類型數組如何轉list?
2.具體分析
在java當中,我們知道:數組轉list的方式是:
List<T> list = Arrays.asList(array);
但是,只知其一不知其二:
通過Arrays.asList方法轉換為List,List的元素必須是包裝類,不能是基本數據類型。
因為基本數據類型無法不能被泛型化,所以,基本數據類型,無法通過這種方式完成從數組到list的轉換。
舉個栗子:
輸出的是byte[]的內存地址,而不是每個byte元素。
已知:
byte[] bytes = new byte[]{1, 2, 3, 4};
short[] shorts = {5, 6, 7, 8};
int[] ints = new int[4];
ints[0] = 9;
ints[1] = 10;
ints[2] = 11;
ints[3] = 12;
long longs[] = new long[]{13,14,15,16};
double doubles[] = {17,18,19,20};
float[] floats = {21,22,23,24};
char[] chars = {'a','b','c','d'};
需要將這些數組,轉list,如何實現?
3.解決方案
方式一:使用封裝類;
我們知道,基本數據類型都有屬於自己的封裝類;
java共有8種基本數據類型,對應的封裝類如下:
基本數據類型 | 對應封裝類 | 封裝類包路徑 |
byte | Byte | java.lang.Byte |
short | Short | java.lang.Short |
int | Integer | java.lang.Integer |
long | Long | java.lang.Long |
double | Double | java.lang.Double |
float | Float | java.lang.Float |
char | Character | java.lang.Character |
boolean | Boolean | java.lang.Boolean |
// 基本數據類型
byte[] bytes = new byte[]{1, 2, 3, 4};
Collections.singletonList(bytes).forEach(t -> System.out.println(t));
// 封裝類
Byte[] bytes2 = new Byte[]{1, 2, 3, 4};
Arrays.asList(bytes2).forEach(System.out::println);
Byte[] bytes2 = new Byte[]{1, 2, 3, 4};
Short[] shorts2 = {5, 6, 7, 8};
Integer[] integers = {9, 10, 11, 12};
Long longs2[] = new Long[]{13L,14L,15L,16L};
Double doubles2[] = {17D,18D,19D,20D};
Float[] floats2 = {21F,22F,23F,24F};
我們可以看到:當int類型轉變成封裝類的時候,轉成Byte,Short,Integer可以自動完成轉換;
而,int轉成Long,Double,Float的時候是需要手動轉換。
方式二:使用java類;
語法:
List<T> list = Arrays.stream(arrays).boxed().collect(Collectors.toList());
java8的Stream,可以將int, long, double三種基本類型轉換成對應的封裝類list。
int[] ints = new int[4];
ints[0] = 9;
ints[1] = 10;
ints[2] = 11;
ints[3] = 12;
Arrays.asList(ints).forEach(System.out::print);
System.out.println();
Arrays.stream(ints).boxed().collect(Collectors.toList()).forEach(System.out::println);
long longs[] = new long[]{13,14,15,16};
Arrays.stream(longs).boxed().collect(Collectors.toList()).forEach(System.out::println);
double doubles[] = {17,18,19,20};
Arrays.stream(doubles).boxed().collect(Collectors.toList()).forEach(System.out::print);
除了不能將byte[]轉成list之外,int,long,double,基本上已經夠用了。
方式三:使用Google封裝的jar包。
所需jar包
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
對照關系
基本數據類型 | 對應google類 | 封裝類包路徑 |
byte | Bytes | com.google.common.primitives.Bytes |
short | Shorts | com.google.common.primitives.Shorts |
int | Ints | com.google.common.primitives.Ints |
long | Longs | com.google.common.primitives.Longs |
double | Doubles | com.google.common.primitives.Doubles |
float | Floats | com.google.common.primitives.Floats |
char | Chars | com.google.common.primitives.Chars |
boolean | Booleans | com.google.common.primitives.Booleans |
語法:
封裝類.asList(對應基本數據類型數組);
示例:
char[] chars = {'a','b','c','d'};
Chars.asList(chars).forEach(System.out::println);
2023年2月10日16:54:50
控制台輸出(打印)byte數組
System.out.println(Arrays.toString(bytes));
寫在最后
哪位大佬如若發現文章存在紕漏之處或需要補充更多內容,歡迎留言!!!