在list集合中的添加、修改、删除和遍历元素
来源:互联网 发布:淘宝网div css布局实例 编辑:程序博客网 时间:2024/06/06 01:41
集合与数组相似,但他的长度是可变的
list集合继承了collection接口,list集合的实现类是ArrayList()。
如下:
package com.test;import java.util.ArrayList;// import the ArrayList packageimport java.util.List;// import the List packagepublic class Test {public List list;//declare List collectionTest(){this.list= new ArrayList();//Implementation of List collection}}
list集合中的元素是有序的,可重复的。
先定义一个Student类
package com.test;public class Student {public String m_name;public int m_id;public Student(int id,String name){this.m_name= name;this.m_id = id;}}
再定义一个类,里面包含main()方法来实现
然后在list集合中添加元素
package com.test;import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class Test {public List<Student> list;public Test(){this.list= new ArrayList<Student>();}//add to studentpublic void TestAdd(){Student stu = new Student(1,"小明");list.add(stu);//add studentStudent stu1= new Student(2,"小李");list.add(0, stu1);//Specify the location to add the studentStudent[] stu2= {new Student(3,"小陈"),new Student(4,"小周")};list.addAll(Arrays.asList(stu2));//Add a number of studentStudent[] stu3= {new Student(5,"小吴"),new Student(1,"小明")};list.addAll(2, Arrays.asList(stu3));//Specify the location to add a number of student}public static void main(String[] args) {Test test = new Test();test.TestAdd();}}
在list集合中遍历元素:
<pre name="code" class="java">
public void TestFor(){int size = list.size();//get the length of the listfor(int i=0; i<size; i++){Student stu = list.get(i);//get element of the listSystem.out.println("student: "+stu.m_id+":"+stu.m_name);}System.out.println();}//Using ForEach traversal elementspublic void TestForEach(){for(Student stu:list){//Get elements from the list to the Student objectSystem.out.println("student: "+stu.m_id+":"+stu.m_name);}System.out.println();}//Using iterator traversal elementpublic void TestIterator(){Iterator it = list.iterator();//Define iterator objectswhile(it.hasNext()){//Whether the judgment is emptyStudent stu = (Student)it.next();//Mandatory conversion of iterator object to the Student objectSystem.out.println("student: "+stu.m_id+":"+stu.m_name);}System.out.println();}public static void main(String[] args) {Test test = new Test();test.TestAdd();<span style="white-space:pre"></span>test.TestFor();<span style="white-space:pre"></span>test.TestForEach();<span style="white-space:pre"></span>test.TestIterator();}
result is:
student: 2:小李<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 2:小李<span style="white-space:pre"></span></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 2:小李</span>student: 1:小明<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 1:小明</span><span style="font-family: Arial, Helvetica, sans-serif;"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 1:小明</span>student: 5:小吴<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 5:小吴<span style="white-space:pre"></span></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 5:小吴</span>student: 1:小明<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 1:小明<span style="white-space:pre"></span></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 1:小明</span>student: 3:小陈<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 3:小陈<span style="white-space:pre"></span></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 3:小陈</span>student: 4:小周<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 4:小周<span style="white-space:pre"></span></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 4:小周</span>
在list集合中修改元素:
<pre name="code" class="java">//modify elementpublic void TestModify(){list.set(3, new Student(6,"小张"));//Replace the element with the specified location in the list with the new elementthis.TestFor();}<pre name="code" class="java">public static void main(String[] args) {Test test = new Test();test.TestAdd();<span></span>test.TestFor();<span></span>test.TestModify();}result is:
student: 2:小李<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 2:小李</span>student: 1:小明<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 1:小明</span>student: 5:小吴<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 5:小吴</span>student: 1:小明<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 6:小张</span>student: 3:小陈<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 3:小陈</span>student: 4:小周<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 4:小周</span>在List集合中删除元素:
<pre name="code" class="java">//delete studentpublic void TestRemove(){list.remove(3);//Remove elements from the specified locationthis.TestFor();list.remove(list.get(0));//Direct delete elementthis.TestFor();Student[] stu = {(Student)list.get(1),(Student)list.get(2)};//get multiple elementslist.removeAll(Arrays.asList(stu));//delete multiple elementsthis.TestFor();}<pre name="code" class="java">public static void main(String[] args) {Test test = new Test();test.TestAdd();<span></span>test.TestFor();<span></span>test.TesRemove();}
result is:
student: 2:小李<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 2:小李<span style="white-space:pre"></span></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 1:小明<span style="white-space:pre"></span></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 1:小明</span>student: 1:小明<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 1:小明<span style="white-space:pre"></span></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 5:小吴<span style="white-space:pre"></span></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 4:小周</span>student: 5:小吴<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 5:小吴<span style="white-space:pre"></span></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 3:小陈</span>student: 1:小明<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 3:小陈<span style="white-space:pre"></span></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 4:小周</span>student: 3:小陈<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">student: 4:小周</span>student: 4:小周
0 0
- 在list集合中的添加、修改、删除和遍历元素
- 如何在遍历list集合中删除list集合中的元素
- 在遍历集合的时候删除集合中的元素
- 遍历删除List中的元素
- 遍历删除List中的元素
- java 遍历集合的同时添加和删除元素
- 【Java】Java遍历删除集合中的元素
- 正确遍历删除List中的元素
- 如何正确遍历删除List中的元素
- 如何正确遍历删除List中的元素
- 如何正确遍历删除List中的元素
- 正确遍历删除List中的元素
- 正确遍历删除List中的元素
- 正确遍历删除List中的元素
- 遍历并删除list中的元素
- 正确在遍历中删除List元素
- 遍历List,删除元素
- java中循环遍历删除List和Set集合中元素的方法
- Retrofit之Query注解
- OSG多线程,多显示场景图形设计
- iOS 当页面中没有数据的时候出现的一个指向创建按钮的 UIView
- Zigbee协议按键触发流程
- start with connect by prior子句实现递归查询
- 在list集合中的添加、修改、删除和遍历元素
- Windows Cython 安装
- Linux useradd 命令
- linux中shell变量$#,$@,$0,$1,$2的含义解释
- unity3d中脚本生命周期(MonoBehaviour lifecycle)
- 【LEETCODE】77-Combinations [Python]
- oracle merge into 用法详解
- 以下是JavaScript容易犯错的九个陷阱。
- 机器学习实战第三章——决策树(源码解析)