Arrays.asList()方法总结 2021-06-24 14:35 296阅读 0赞 1、该方法对于基本数据类型的数组支持并不好,当数组是基本数据类型时不建议使用 : public static void test1 () { int[] a_int = { 1, 2, 3, 4 }; /* 预期输出应该是1,2,3,4,但实际上输出的仅仅是一个引用, 这里它把a_int当成了一个元素 */ List a_int_List = Arrays.asList(a_int); for (int i=0 ;i<a_int_List.size();i++ ) { System.out.println(a_int_List.get(i)); } } 输出:\[I@4ec4d412 当数组是对象类型时,可以得到我们的预期: public static void test2 () { Integer[] a_int = { 1, 2, 3, 4 }; List a_int_List = Arrays.asList(a_int); for (int i=0 ;i<a_int_List.size();i++ ) { System.out.println(a_int_List.get(i)); } } 输出: 1 2 3 4 2、当使用asList()方法时,数组就和列表链接在一起了。当更新其中之一时,另一个将自动获得更新。 注意:仅仅针对对象数组类型,基本数据类型数组不具备该特性 public static void test3 () { Integer[] a_int = { 1, 2, 3, 4 }; /* 预期输出应该是1,2,3,4,但实际上输出的仅仅是一个引用, 这里它把a_int当成了一个元素 */ List<Integer> a_int_List = Arrays.asList(a_int); a_int_List.set(0, 10); a_int[1]=0; for (int i=0 ;i<a_int_List.size();i++ ) { System.out.println(a_int_List.get(i)); } System.out.println(); for (Integer i : a_int) { System.out.println(i); } } 输出: 10 0 3 4 10 0 3 4 3、asList得到的List是Arrays内部类的List,没有add和remove方法(方法很少) public static void test3 () { Integer[] a_int = { 1, 2, 3, 4 }; /* 预期输出应该是1,2,3,4,但实际上输出的仅仅是一个引用, 这里它把a_int当成了一个元素 */ List<Integer> a_int_List = Arrays.asList(a_int); a_int_List.add(0); for (int i=0 ;i<a_int_List.size();i++ ) { System.out.println(a_int_List.get(i)); } System.out.println(); for (Integer i : a_int) { System.out.println(i); } } 当调用a\_int\_List.add(0);时,会报错。
相关 vue方法总结 一:什么是mvvm mvvm特点 ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9i 悠悠/ 2021年09月02日 03:07/ 0 赞/ 344 阅读
相关 常见方法总结 js判断是哪个浏览器 var inBrowser = typeof window !== 'undefined'; var inWeex = typeof WXEn 港控/mmm°/ 2021年09月30日 11:52/ 0 赞/ 248 阅读
相关 Assert方法总结 junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类。 1.assertTrue/False(\[String messag 客官°小女子只卖身不卖艺/ 2021年11月16日 13:38/ 0 赞/ 198 阅读
相关 学习方法总结 [2019独角兽企业重金招聘Python工程师标准>>> ][2019_Python_] ![hot3.png][] ![4206b113ef8905627a1c541aab 喜欢ヅ旅行/ 2022年01月14日 22:13/ 0 赞/ 106 阅读
相关 数组方法总结 1.shift() 方法:把数组的第一个元素删除,并返回第一个元素的值 var movePos=\[11,22\]; movePos.shift() console. 客官°小女子只卖身不卖艺/ 2022年03月24日 19:14/ 0 赞/ 139 阅读
相关 finalize()方法总结 class Object { ... protected void finalize() throws Throwable { } 系统管理员/ 2022年05月16日 01:20/ 0 赞/ 106 阅读
相关 String方法总结 1.charAt() 返回指定索引处的字符。索引范围为从 0 到 length() - 1 String s="1234567890"; char t=s 骑猪看日落/ 2022年05月17日 11:16/ 0 赞/ 72 阅读
相关 js方法总结 substring(n) 截取从第n位开始到结尾 str.split(); 字符串分割成字符串数组 str. 心已赠人/ 2022年07月12日 14:07/ 0 赞/ 148 阅读
相关 学习方法总结 如何学习一门新的技术呢,其实是有方法可以遵循的,可以有条理的快速入门。我的方法如下: 1: 它是什么 a):看定义(google、baidu) b):同类或者类 我不是女神ヾ/ 2022年07月13日 14:00/ 0 赞/ 50 阅读
还没有评论,来说两句吧...