Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java List interface and Iterator interface and the method of foreach recycling

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "Java List interface and Iterator interface and the method of foreach recycling". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "Java List interface and Iterator interface and foreach recycling method" can help you solve the problem.

List interface

The List interface inherits the Collection interface and belongs to 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 by index, and the elements are stored sequentially, that is, the storage order of the elements is the same as that of the elements taken out.

ArrayList collection

ArrayList is an implementation class of the List interface that encapsulates an array object of variable length inside ArrayList.

Package collection class; 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 ("length of collection:" + list.size ()) System.out.println ("second element is:" + list.get (1));}}

Running result

As can be seen from the running results, both ArrayList and array indexes start from 0. Because the bottom of the ArrayList collection uses an array to save elements, when adding or deleting elements in a specified location, it will lead to the creation of a new array, which is relatively inefficient, so it is not suitable for a large number of add and delete operations, but you can find elements through the index, which also increases the efficiency of finding elements.

LinkedList collection

In order to solve the problem that the ArrayList collection is inefficient in adding and deleting elements, the LinkList collection is introduced, and a two-way circular linked list is maintained inside the LinkList collection. Each element of the linked list uses references to remember the former element and the latter element, so all elements can be connected. When you insert a new element, you only need to modify the reference relationship between the elements. This increases the efficiency of adding and deleting elements.

Package collection class; 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"); / / print the element System.out.println (link.toString ()) in the collection / / insert the element link.add (3, "Student") in the collection; / / insert the element link.addFirst ("Li long"); System.out.println (link) in the first position of the collection; / / take out the first element in the collection, System.out.println (link.getFirst ()) / / delete the element link.remove (3) in the collection; / / delete the first element in the collection, link.removeFirst (); System.out.println (link);}}

Running result

Iterator interface

Iterator is mainly used to traverse elements in Collection, and Iterator is also known as an iterator.

Package collection class; import java.util.ArrayList; import java.util.Iterator; public class DieDai {public static void main (String [] args) {ArrayList list=new ArrayList (); list.add ("study hard"); list.add ("be an excellent successor to communism"); list.add ("serve the people"); list.add ("be a useful person to society") Iterator it= list.iterator (); / / determine whether the next element while (it.hasNext ()) {Object obj=it.next (); System.out.println (obj);} exists in ArrayList.

Running result

In the process of Iterator traversal, the hasNext () method is used to determine whether there is the next element in the collection, and if there is an element, the element is extracted by the next () method.

Foreach cycle

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

Package collection class; 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 result

As you can see, the enhanced for loop is very convenient in the process of traversing the collection, there are no loop conditions and no iterations, the number of loops is determined by the number of elements in the loop, and each loop, the enhanced for loop remembers the elements of the current loop through variables, thus printing out the elements in the collection respectively.

This is the end of the introduction of "Java List interface and Iterator interface and the method of foreach recycling". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report