Arrays.asList() 示例

2023-05-20,,

 1 package Test.others;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.List;
6
7 public class ArraysDemo {
8 public static void main(String[] args) {
9 /*
10 static <T> List<T> asList(T... a)
11 返回由指定数组支持的固定大小的列表。
12 1.参数列表是一个可变参数列表 数量任意 类型一致即可
13 2.T泛型仅仅支持对象类型 基本数据类型进入 自动成了包装类
14 3.固定大小的列表
15 */
16 /*1.放基本数据类型*/
17 int[] a_int_arr = {9, 8, 7, 6, 5, 7, 3};
18 List<int[]> a_int_List = Arrays.asList(a_int_arr);//放入了一个参数 .var 自动补全
19 //这样放的话 是一个放一个int数组对象
20 System.out.println(a_int_List);//直接打打印数组对象 数组对象没有重写toString
21 for (int[] ints : a_int_List) {
22 System.out.println(Arrays.toString(ints));
23 }
24 System.out.println("-----------------------");
25 /*2.放引用类型*/
26 Integer[] a_Integer_arr = new Integer[]{12, 13, 15, 2, 6, 5, 7};//自动装箱
27 List<Integer> a_Integer_List = Arrays.asList(a_Integer_arr);//放入了一个引用数据类型的数组
28 List<Integer> a_Integer_List_toLong = Arrays.asList(12, 13, 15, 2, 6, 5, 7);//自动装箱成包装类 加入8个数
29 System.out.println(a_Integer_List);
30 System.out.println(a_Integer_List_toLong);
31 System.out.println("-----------------------");
32 /*3.对转化后的list 使用修改方法*/
33 /*
34 * 修改后 两个引用都是指向了同一对象 所以都更新了
35 *
36 */
37 a_Integer_List.set(0, 123);
38 System.out.println("IntegerList:"+a_Integer_List);//元素重写了toString() 获得不了地址值了-.-...
39 System.out.println(a_Integer_arr);//同样 Integer数组对象 也没有重写 toString()
40 System.out.println("Integer数组:"+Arrays.toString(a_Integer_arr));
41 //Integer数组: 值和 IntegerList值同时变是对的
42 // 可变参数的本质就是一个数组 用对象数组 传给 另外的一个对象数组 传的是地址值 改一处 持有同样地址的变量 展示数据一致
43 System.out.println(a_Integer_arr.hashCode());// 数组对象
44 System.out.println(a_Integer_List.hashCode());// list 对象 不相同是对的
45 System.out.println("-----------------------");
46 /*4.使用Add remove*/
47 /*
48 * 不支持的操作异常
49 *
50 * */
51 // a_Integer_List.add(1);
52 // Integer one= 7;
53 // System.out.println(a_Integer_List.remove(one));
54 /*5.看看基本数据类型能不能用List的方法改值*/
55 /*
56 *用set是能改值的 只不过和你想的不一样 改的话是整体改
57 *int[]直接转List 放入的是一个整体 仅且一个 数组对象
58 *a_int_List 和 a_int_arr 指向的不是同一个对象所以
59 *
60 */
61 // a_int_List.set(0,123);//要的一个int数组对象 提供的是 int
62
63 for (int[] ints : a_int_List) {
64 System.out.print("修改前:");
65 System.out.println(Arrays.toString(ints));
66 }
67 a_int_List.set(0, new int[]{1, 2, 3});
68 for (int[] ints : a_int_List) {
69 System.out.print("修改后:");
70 System.out.println(Arrays.toString(ints));
71 }
72 }
73 }

Arrays.asList() 示例的相关教程结束。

《Arrays.asList() 示例.doc》

下载本文的Word格式文档,以方便收藏与打印。