当前位置:网站首页>用map集合或者list集合存放list集合
用map集合或者list集合存放list集合
2022-06-21 10:46:00 【Yield & Allure】
1.用list集合存放list
注意:
在每一次往List<List>中添加list时,都应该重新在内存中开辟一个list空间便于给存放新的数据到list中
List<List>存放list类型的数据要使用用add(int i,list)方法而不是add(list),这样的话在每次new一个list集合时,List<List>都只存放一个数据,之前的list数据会被覆盖(还是list在开辟的内存中指向的问题),只输出最后一次循环的数据。所以要规定每次这个数据要存放到哪里,使用add(int i,list)方法。
要给list存放数据时,每次都要new一个list,不new的话list在内存中只开辟一个list对象,每次往里面存放数据时,list都指向这一个对象。所以要达到每次循环都有不一样的list时,在循环下面重新在内存中开辟新的list
public static void main(String[] args) {
List<List<User>> list = new ArrayList<>();
for (int i = 0; i < 2; i++) {
List<User> list1 = new ArrayList<>();
list1.add(new User("1","zhangsan"));
list1.add(new User("2","lisi"));
list1.add(new User("3","wangwu"));
list.add(list1);
}
System.out.println("list: "+list);
}运行结果

不能写成这样
public static void main(String[] args) {
List<List<User>> list = new ArrayList<>();
List<User> list1 = new ArrayList<>();
for (int i = 0; i < 2; i++) {
list1.add(new User("1","zhangsan"));
list1.add(new User("2","lisi"));
list1.add(new User("3","wangwu"));
list.add(list1);
}
System.out.println("list: "+list);
}否则结果为:
则会出现以上问题,每次按照定义的循环add完之后才能添加(会导致重复数据)
2.用map集合存储list
//当添加多个值的时候:
public static void main(String[] args) {
List<List<User>> list = new ArrayList<>();
Map<String,List<User>> map = new HashMap<>();
for (int i = 0; i < 2; i++) {
List<User> list2 = new ArrayList<>();
list2.add(new User("1","zhangsan"));
list2.add(new User("2","lisi"));
list2.add(new User("3","wangwu"));
map.put("list"+i,list2);
}
}
//当添加一个list集合时:
public static void main(String[] args) {
List<List<User>> list = new ArrayList<>();
Map<String,List<User>> map = new HashMap<>();
List<User> list2 = new ArrayList<>();
list2.add(new User("1","zhangsan"));
list2.add(new User("2","lisi"));
list2.add(new User("3","wangwu"));
map.put("list"+i,list2);
}
多个list值的结果

一个list值的结果

边栏推荐
- K-means introduction
- leetcode-94-二叉树的中序遍历
- MySQL 5.7 is about to be stopped and only maintained. It's time to learn a wave of MySQL 8
- Mythical Games宣布与韩国领先游戏发行商Kakao Games合作,推动亚太区业务扩张
- grant all privileges on
- 我国服装纺织制造业面临着异常严峻的挑战
- TensorFlow,危!抛弃者正是谷歌自己
- Financial institutions scramble for "digital employees"; Release of aging service evaluation framework for insurance app
- cvte一面
- 2. MySQL index optimization
猜你喜欢

MySQL - library operation

The most powerful eight part essay in 2022, "code out eight part essay - cut out the offer line"

东方甄选双语直播火爆出圈,新东方转型初见端倪

性能优化——图片压缩、加载和格式选择

Mqtt of NLog custom target

Summary of embedded development -- General Catalog

芯片供给过剩迹象明显,ASML不再是香饽饽,投资机构大举做空

Feature metric loss for self supervised learning of depth and egomotion

Xidian AI ranked higher than Qingbei in terms of AI major, and Nantah ranked first in China in terms of Software Science in 2022

Running view of program
随机推荐
聊聊大火的多模态项目
Simple implementation of Snake game in C language
Application configuration management, basic principle analysis
Three elements of basic concepts and methods of machine learning
Will the thunderstorm of Celsius be the "Lehman moment" in the field of encryption?
Answers to mobile application development learning general test questions
Actual measurement: the performance of cloud RDS MySQL is 1.6 times that of self built
One of the components of the program
js正则-梳理
Dapr advanced -02- view dapr logs
Revenue of Fortinet will increase by 29% in 2021 founder Xieqing talks about the future of network security industry
ThreadLocal
Start from scratch 10- background management system development
MySQL 5.7 is about to be stopped and only maintained. It's time to learn a wave of MySQL 8
Add solid state to the computer
Wangedit encapsulation plug-in preliminary
ESP8266/ESP32 +1.3“ or 0.96“ IIC OLED指针式时钟
Haplotype analysis using shapeit
Do website from scratch 11- blog development
DSP online upgrade (4) -- functions implemented by bootloader