当前位置:网站首页>List删除集合元素
List删除集合元素
2022-07-27 08:25:00 【北海里的避港湾】
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Student {
private String name;
private String age;
}
public static void main(String[] args) {
iteratorDelete();
// listDelete();
}
/** * list.remove()删除会报错 */
private static void listDelete() {
List<Student> cityList = setValue();
System.out.println(cityList.size());
for (Student student : cityList) {
if(student.getAge().equals("xx")){
cityList.remove(student);
}
}
System.out.println(cityList.size());
}
/** * list删除数据使用迭代器删除 */
private static void iteratorDelete() {
List<Student> cityList = setValue();
ListIterator<Student> iterator =cityList.listIterator();
while (iterator.hasNext()) {
Student student = iterator.next();
if(student.getAge().equals("xx")){
System.out.println("删除的是集合的数据,对iterator没有影响");
iterator.remove();
}
System.out.println("删除后的长度:"+cityList.size());
int i = iterator.nextIndex();
int previousIndex = iterator.previousIndex();
System.out.println(student + "==" + i + "==" + previousIndex);
}
System.out.println(cityList.size());
}
private static List<Student> setValue(){
List<Student> cityList = new ArrayList<Student>();
cityList.add(new Student("xx", "xx"));
cityList.add(new Student("qq", "qq"));
cityList.add(new Student("ww", "ww"));
cityList.add(new Student("ee", "ee"));
cityList.add(new Student("rr", "rr"));
cityList.add(new Student("tt", "tt"));
return cityList;
}
边栏推荐
- Translation character '/b' in C #
- Luogu super Mary game
- Attack and defense World Lottery
- Stored procedure test 1 -- first acquaintance of love
- Data extraction 1
- Containerd failed to pull private database image (kubelet)
- Iterators and generators
- 2022-07-26 group 4 abstract classes and interfaces
- UVM入门实验1
- [ciscn2019 southeast China division]web11 1
猜你喜欢

Node installation and debugging

Flutter 渲染机制——GPU线程渲染

Record a PG master-slave setup and data synchronization performance test process

Download and usage of sequel Pro

Teach you to build a nail warning robot hand in hand

PHP realizes data interaction with MySQL

vCenter7.0管理Esxi7.0主机

Plato farm is expected to further expand its ecosystem through elephant swap
![[netding cup 2020 rosefinch group]nmap 1 two solutions](/img/fa/b1349cb42b5768b7510217239ba73a.png)
[netding cup 2020 rosefinch group]nmap 1 two solutions

SSTI template injection
随机推荐
Leetcode56. Consolidation interval
面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
ERP production operation control Huaxia
Weekly learning summary
Virtual machine cloning
我用字符画出了一个谷爱凌!
arguments
百人参与,openGauss开源社区这群人都在讨论什么?
DEMO:ST05 找文本ID 信息
Solve the problem of slow batch insertion of MySQL JDBC data
QT creator code style plug-in beautifier
On Valentine's day, I drew an object with characters!
数据提取2
Download and usage of sequel Pro
while Loop
I drew a Gu ailing with characters!
vCenter7.0管理Esxi7.0主机
How to uninstall -- Qianxin secure terminal management system
[uni app advanced practice] take you hand-in-hand to learn the development of a purely practical complex project 1/100
Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem