Java 集合 List Arrays.asList

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

 参考链接:阿里巴巴Java开发手册终极版v1.3.0

【强制】使用工具类 Arrays.asList()把数组转换成集合时,不能使用其修改集合相关的方
法,它的 add/remove/clear 方法会抛出 UnsupportedOperationException 异常。
说明:asList 的返回对象是一个 Arrays 内部类,并没有实现集合的修改方法。Arrays.asList
体现的是适配器模式,只是转换接口,后台的数据仍是数组。String[] str = new String[] { "you", "wu" };List list = Arrays.asList(str);
第一种情况:list.add("yangguanbao"); 运行时异常。
第二种情况:str[0] = "gujin"; 那么 list.get(0)也会随之修改。

使用Array.asList()初始化一个List集合,就不能再添加新的元素,因为初始化的是一个固定大小的数组,来看Arrays源码。

221828_QHRd_3781047.png

List<String> seasons = Arrays.asList("Spring", "Summer", "Fall");
seasons.add("Winter");
System.out.println("seasons:"+seasons.toString());

运行结果:

Exception in thread "main" java.lang.UnsupportedOperationExceptionat java.util.AbstractList.add(Unknown Source)at java.util.AbstractList.add(Unknown Source)at com.jerry.entity.ListDemo.main(ListDemo.java:10)

在Java 7以后,实现集合里面不必写明具体的元素类型。

222413_nv4g_3781047.png

ArrayList的两种用法:

package com.jerry.entity;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;public class ListDemo {public static void main(String[] args) {List<String> seasons = Arrays.asList("Spring", "Summer", "Fall");
//		seasons.add("Winter"); // throw UnsupportedOperationExceptionSystem.out.println("seasons:"+seasons.toString());List<String> seasons2 = new ArrayList<>(seasons);seasons2.add("Winter");System.out.println("seasons2:"+seasons2.toString());List<String> seasons3 = new ArrayList<>();seasons3.add("Spring");seasons3.add("Summer");seasons3.add("Fall");seasons3.add("Winter");System.out.println("seasons3:"+seasons3.toString());// java 8List<String> seasons4 = Stream.of("Spring", "Summer", "Fall").collect(Collectors.toList());System.out.println("seasons4:"+seasons4.toString());}}

运行结果:

seasons:[Spring, Summer, Fall]
seasons2:[Spring, Summer, Fall, Winter]
seasons3:[Spring, Summer, Fall, Winter]
seasons4:[Spring, Summer, Fall]

List<String>的几种用法:

225643_YWk4_3781047.png

转载于:https://my.oschina.net/u/3781047/blog/1627493

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391147.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

重学TCP协议(9) 半连接队列、全连接队列

1. 半连接队列、全连接队列基本概念 三次握手中&#xff0c;在第一步server收到client的syn后&#xff0c;把相关信息放到半连接队列中&#xff0c;同时回复synack给client&#xff08;第二步&#xff09;&#xff0c;同时开启一个定时器&#xff0c;如果超时还未收到 ACK 会进…

分类预测回归预测_我们应该如何汇总分类预测?

分类预测回归预测If you are reading this, then you probably tried to predict who will survive the Titanic shipwreck. This Kaggle competition is a canonical example of machine learning, and a right of passage for any aspiring data scientist. What if instead …

【CZY选讲·Yjq的棺材】

题目描述 Yjq想要将一个长为宽为的矩形棺材&#xff08;棺材表面绝对光滑&#xff0c;所以棺材可以任意的滑动&#xff09;拖过一个L型墓道。 如图所示&#xff0c;L型墓道两个走廊的宽度分别是和&#xff0c;呈90&#xff0c;并且走廊的长度远大于。 现在Hja想知道对于给…

“机器换人”之潮涌向珠三角,蓝领工人将何去何从

企业表示很无奈&#xff0c;由于生产需要&#xff0c;并非刻意换人。 随着传统产业向更加现代化、自动化的新产业转型&#xff0c;“机器换人”似乎是历史上不可逆转的潮流。 据报道&#xff0c;珠三角经济圈所在的广东省要从传统的制造大省向制造强省转变&#xff0c;企业转型…

slack通知本地服务器_通过构建自己的Slack App学习无服务器

slack通知本地服务器Serverless architecture is the industrys latest buzzword and many of the largest tech companies have begun to embrace it. 无服务器架构是业界最新的流行语&#xff0c;许多大型科技公司已开始采用它。 In this article, well learn what it is an…

深入理解InnoDB(6)—独立表空间

InnoDB的表空间 表空间可以看做是InnoDB存储引擎逻辑结构的最高层 &#xff0c;所有的数据都是存放在表空间中。 1. Extent 对于16KB的页来说&#xff0c;连续的64个页就是一个区&#xff0c;也就是说一个区默认占用1MB空间大小。 每256个区被划分成一组,第一组的前3个页面是…

神经网络推理_分析神经网络推理性能的新工具

神经网络推理Measuring the inference time of a trained deep neural model on different hardware devices is a critical task when making deployment decisions. Should you deploy your inference on 8 Nvidia V100s, on 12 P100s, or perhaps you can use 64 CPU cores?…

Eclipse断点调试

1.1 Eclipse断点调试概述Eclipse的断点调试可以查看程序的执行流程和解决程序中的bug1.2 Eclipse断点调试常用操作:A:什么是断点&#xff1a;就是一个标记&#xff0c;从哪里开始。B:如何设置断点&#xff1a;你想看哪里的程序&#xff0c;你就在那个有效程序的左边双击即可。C…

react部署在node_如何在没有命令行的情况下在3分钟内将React + Node应用程序部署到Heroku

react部署在nodeIn this tutorial we will be doing a basic React Node app deploy to Heroku. 在本教程中&#xff0c;我们将进行基本的React Node应用程序部署到Heroku。 There are a lot of tutorials that do this only using the command line, so to change things u…

深入理解InnoDB(7)—系统表空间

系统表空间 可以看到&#xff0c;系统表空间和独立表空间的前三个页面&#xff08;页号分别为0、1、2&#xff0c;类型分别是FSP_HDR、IBUF_BITMAP、INODE&#xff09;的类型是一致的&#xff0c;只是页号为3&#xff5e;7的页面是系统表空间特有的 页号3 SYS: Insert Buffer …

CodeForces - 869B The Eternal Immortality

题意&#xff1a;已知a,b&#xff0c;求的最后一位。 分析&#xff1a; 1、若b-a>5&#xff0c;则尾数一定为0&#xff0c;因为连续5个数的尾数要么同时包括一个5和一个偶数&#xff0c;要么包括一个0。 2、若b-a<5&#xff0c;直接暴力求即可。 #include<cstdio>…

如何在24行JavaScript中实现Redux

90% convention, 10% library. 90&#xff05;的惯例&#xff0c;10&#xff05;的图书馆。 Redux is among the most important JavaScript libraries ever created. Inspired by prior art like Flux and Elm, Redux put JavaScript functional programming on the map by i…

卡方检验 原理_什么是卡方检验及其工作原理?

卡方检验 原理As a data science engineer, it’s imperative that the sample data set which you pick from the data is reliable, clean, and well tested for its usability in machine learning model building.作为数据科学工程师&#xff0c;当务之急是从数据中挑选出的…

Web UI 设计(网页设计)命名规范

Web UI 设计命名规范 一.网站设计及基本框架结构: 1. Container“container“ 就是将页面中的所有元素包在一起的部分&#xff0c;这部分还可以命名为: “wrapper“, “wrap“, “page“.2. Header“header” 是网站页面的头部区域&#xff0c;一般来讲&#xff0c;它包含…

leetcode 1486. 数组异或操作(位运算)

给你两个整数&#xff0c;n 和 start 。 数组 nums 定义为&#xff1a;nums[i] start 2*i&#xff08;下标从 0 开始&#xff09;且 n nums.length 。 请返回 nums 中所有元素按位异或&#xff08;XOR&#xff09;后得到的结果。 示例 1&#xff1a; 输入&#xff1a;n …

27个机器学习图表翻译_使用机器学习的信息图表信息组织

27个机器学习图表翻译Infographics are crucial for presenting information in a more digestible fashion to the audience. With their usage being expanding to many (if not all) professions like journalism, science, and research, advertisements, business, the re…

在HTML中使用javascript (js高级程序设计)

在HTML中使用javascript 刚开始入门的时候觉得关于应用以及在html中只用javascript很简单&#xff0c;不需要进行学习。我又开始重温了一下红宝书&#xff0c;觉得还是有必要进行学习的。这是一个笔记&#xff01; script 元素插入有多种方式 属性使用方式async延迟脚本&#x…

大数据新手之路二:安装Flume

Ubuntu16.04Flume1.8.0 1.下载apache-flume-1.8.0-bin.tar.gz http://flume.apache.org/download.html 2.解压到/usr/local/flume中 3.设置配置文件/etc/profile文件&#xff0c;增加flume的路径 ①vi /etc/profile export FLUME_HOME/usr/local/flume export PATH$PATH:$FLUME…

leetcode 1723. 完成所有工作的最短时间(二分+剪枝+回溯)

给你一个整数数组 jobs &#xff0c;其中 jobs[i] 是完成第 i 项工作要花费的时间。 请你将这些工作分配给 k 位工人。所有工作都应该分配给工人&#xff0c;且每项工作只能分配给一位工人。工人的 工作时间 是完成分配给他们的所有工作花费时间的总和。请你设计一套最佳的工作…

异步解耦_如何使用异步生成器解耦业务逻辑

异步解耦Async generators are new in JavaScript. They are a remarkable extension. They provide a simple but very powerful tool for splitting programs into smaller parts, making sources easier to write, read, maintain and test.异步生成器是JavaScript中的新增功…