在list集合中的新增、修改、刪除和遍歷元素
阿新 • • 發佈:2019-02-09
集合與陣列相似,但他的長度是可變的
list集合繼承了collection介面,list集合的實現類是ArrayList()。
如下:
package com.test; import java.util.ArrayList; // import the ArrayList package import java.util.List; // import the List package public class Test { public List list; //declare List collection Test(){ 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 student public void TestAdd(){ Student stu = new Student(1,"小明"); list.add(stu); //add student Student stu1= new Student(2,"小李"); list.add(0, stu1); //Specify the location to add the student Student[] stu2= {new Student(3,"小陳"),new Student(4,"小周")}; list.addAll(Arrays.asList(stu2)); //Add a number of student Student[] 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 list for(int i=0; i<size; i++){ Student stu = list.get(i); //get element of the list System.out.println("student: "+stu.m_id+":"+stu.m_name); } System.out.println(); } //Using ForEach traversal elements public void TestForEach(){ for(Student stu:list){ //Get elements from the list to the Student object System.out.println("student: "+stu.m_id+":"+stu.m_name); } System.out.println(); } //Using iterator traversal element public void TestIterator(){ Iterator it = list.iterator(); //Define iterator objects while(it.hasNext()){ //Whether the judgment is empty Student stu = (Student)it.next(); //Mandatory conversion of iterator object to the Student object System.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 element
public void TestModify(){
list.set(3, new Student(6,"小張")); //Replace the element with the specified location in the list with the new element
this.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 student
public void TestRemove(){
list.remove(3); //Remove elements from the specified location
this.TestFor();
list.remove(list.get(0)); //Direct delete element
this.TestFor();
Student[] stu = {(Student)list.get(1),(Student)list.get(2)}; //get multiple elements
list.removeAll(Arrays.asList(stu)); //delete multiple elements
this.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:小周