java - How can I get a copy of a ArrayList() object instad of a refrence;
问题描述
if I ge
ArrayList<String> myList;
// add some stuff to myList
ArrayList<String> copyOf=myList;
copy will be a reference to myList, thus if I change copyOf, myList will change to. Hpw can I make coyyOf be a copy of myList, so if copyOf chnages, myList will not change.
解决方案
使用将ArrayList
集合作为参数的构造函数。
List<String> myCopy = new ArrayList<>(theArrayListToCopy);
根据 Javadoc,这“构造一个包含指定集合元素的列表,按集合迭代器返回的顺序排列。”
推荐阅读
- nestjs - How to integrate TypeORM with schematics
- java - 无法使用 jdk11 在 ubuntu 上运行我的 spring 应用程序
- node.js - How to get basic Shopify GraphQL Admin API working with nodeJS?
- spring - 如何从 Spring Boot 应用程序属性加载 AWS 区域特定属性?
- python - 使用烧瓶在html表的当前行中传递数据
- cordova - 科尔多瓦 - 未能建立电子
- html - 有没有办法将 CSS 省略号转换为链接?
- php - PHP子字符串函数在末尾返回奇数符号
- reactjs - Firebase,NextJs:如果用户未登录所选页面,如何将用户重定向到登录页面?
- php - 结果未从 PHP API、MySQL 到 Android Studio 提取到 Spinner