当前位置:网站首页>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;
}
边栏推荐
- 百人参与,openGauss开源社区这群人都在讨论什么?
- Breadth first search
- After installing mysql, docker entered the container and found that he could not log in to MySQL
- 【目标检测】YOLOv6理论解读+实践测试VisDrone数据集
- Graph node deployment and testing
- OPPO 自研大规模知识图谱及其在数智工程中的应用
- Promise details
- [target detection] yolov6 theoretical interpretation + practical test visdrone data set
- Introduction, installation and use of netdata performance monitoring tool
- Virtual machine cloning
猜你喜欢

Bandwidth and currency
![[applet] the upload of the wechat applet issued by uniapp failed error: error: {'errcode': -10008,'errmsg':'Invalid IP](/img/0c/da2ffc00834793c7abc0f7ebe6321b.png)
[applet] the upload of the wechat applet issued by uniapp failed error: error: {'errcode': -10008,'errmsg':'Invalid IP

说透缓存一致性与内存屏障

Solve the problem of slow batch insertion of MySQL JDBC data

vCenter7.0管理Esxi7.0主机

Attack and defense world MFW

An ordinary autumn recruitment experience

ERP生产作业控制 华夏

数据提取1

借生态力量,openGauss突破性能瓶颈
随机推荐
Lua iterator
Database startup report error_ user_ connect_ times &gt; 0 error
[uni app advanced practice] take you hand-in-hand to learn the development of a purely practical complex project 1/100
Vcenter7.0 managing esxi7.0 hosts
[BJDCTF2020]EasySearch 1
Local Oracle reported ora-12514: tns: the listener cannot recognize the requested service at present
Use of elastic box / expansion box (Flex)
数据库启动报error_user_connect_times &gt; 0错误
Qt Creator代码风格插件Beautifier
I can't figure out why MySQL uses b+ trees for indexing?
while Loop
JS basic knowledge - daily learning summary ①
"Basic knowledge of PHP" implement mathematical operations in PHP
Data extraction 2
Functions and arrow functions
Solution to the program design of the sequence structure of one book (Chapter 1)
Luogu super Mary game
Map structure
Use of string type "PHP Basics"
Dirsearch[directory scanning tool]