Home  >  Article  >  Java  >  How to use Java List interface, Iterator interface and foreach loop

How to use Java List interface, Iterator interface and foreach loop

王林
王林forward
2023-04-20 17:31:081542browse

List interface

The List interface inherits the Collection interface and is a single-column collection. Duplicate elements are allowed in the List collection. All elements are stored in a linear manner. In the program, the specified elements in the collection are accessed through indexes. The elements are stored sequentially, that is, the order in which the elements are stored is consistent with the order in which they are retrieved.

ArrayList collection

ArrayList is an implementation class of the List interface, which encapsulates a variable-length array object inside ArrayList.

 package 集合类;
 import java.util.ArrayList;
 import java.util.Arrays;
 public class FengZhuanglei {
     public static void main(String[] args) {
         ArrayList list=new ArrayList();
         list.add("stu1");
         list.add("stu2");
         list.add("stu3");
         list.add("stu4");
         System.out.println("集合的长度:"+list.size());
         System.out.println("第二个元素是:"+list.get(1));
     }
 }

Running result

How to use Java List interface, Iterator interface and foreach loop

It can be seen from the running result that ArrayList and array index both start from 0, because the bottom layer of ArrayList collection uses an array to When saving elements, adding or deleting elements at a specified position will result in the creation of a new array, which is relatively inefficient, so it is not suitable for a large number of additions and deletions. However, elements can be found through indexing, which also increases the difficulty of finding elements. s efficiency.

LinkedList collection

In order to solve the problem of low efficiency in adding and deleting elements in the ArrayList collection, the LinkList collection is introduced. A two-way circular linked list is maintained inside the LinkList collection. Each element of the linked list They all use references to remember their previous and next elements, so all the elements can be connected. When inserting a new element, you only need to modify the reference relationship between the elements, which can increase the number of elements. Efficiency of add and delete operations.

 package 集合类;
 import java.util.LinkedList;
 public class LianBiao {
     public static void main(String[] args) {
         LinkedList link =new LinkedList();
         link.add("stu1");
         link.add("stu2");
         link.add("stu3");
         link.add("stu4");
         //打印集合中的元素
         System.out.println(link.toString());
         //在集合中插入元素
         link.add(3,"Student");
         //在集合的第一个位置插入元素
         link.addFirst("李龙");
         System.out.println(link);
         //取出集合中的第一个元素
         System.out.println(link.getFirst());
         //删除集合中的元素
         link.remove(3);
         //删除集合中的第一个元素
         link.removeFirst();
         System.out.println(link);
     }
 }

Running result

How to use Java List interface, Iterator interface and foreach loop

Iterator interface

Iterator is mainly used to traverse the elements in Collection. Iterator is also called iterator .

 package 集合类;
 import java.util.ArrayList;
 import java.util.Iterator;
 public class DieDai {
     public static void main(String[] args) {
         ArrayList list=new ArrayList();
         list.add("好好学习");
         list.add("做一个优秀的共产主义接班人");
         list.add("为人民服务");
         list.add("做一个对社会有用的人");
         Iterator it= list.iterator();
         //判断ArrayList是否存在下一个元素
         while(it.hasNext()){
             Object obj=it.next();
             System.out.println(obj);
         }
     }
 }

Running result

How to use Java List interface, Iterator interface and foreach loop

##In the process of Iterator traversal, first use the hasNext() method to determine whether there is still the next element in the collection. If There is an element, and the element is taken out through the next() method.

foreach loop

Enhance the for loop. The foreach loop does not need to obtain the length of the loop, nor does it need to access the elements in the loop through indexes. foreach will automatically traverse each element in the loop. element.

 package 集合类;
 import java.util.ArrayList;
 public class FE {
     public static void main(String[] args) {
         ArrayList list=new ArrayList();
         list.add("hello");
         list.add("world");
         for(Object obj: list){
             System.out.println(obj);
         }
     }
 }

Running results

How to use Java List interface, Iterator interface and foreach loop

It can be seen that the enhanced for loop is very convenient in the process of traversing the collection. There is no loop condition, no iteration, and loop The number of times is determined by the number of elements in the loop. Each time it loops, the enhanced for loop remembers the elements of the current loop through variables, thereby printing out the elements in the set respectively.

The above is the detailed content of How to use Java List interface, Iterator interface and foreach loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete