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

What are the methods of traversing all elements in the Java collection

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

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "Java collection traversing all the elements of the method have", the article explained the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Java collection traversing all the elements of the method there are" it!

1. For loop statement

The first method is the well-known for loop statement:

Intl = students.size (); System.out.println ("Student Information:"); for (int I = 0; I < l; iTunes +) {Student s = (Student) students.get (I) System.out.println ("serial number:" + s.ID + "\ t\ t\ t name:" + s.name + "\ t\ t\ t age:" + s.age + "\ t\ t gender:" + s.gender);}} 2, foreach statement

The foreach statement is one of the new features of the Java5 version, which provides programmers with great convenience when it is designed to traverse arrays and collections, but the foreach statement cannot completely replace the for loop statement.

System.out.println ("(foreach) student information:"); for (Object obj:students) {Student s = (Student) obj; System.out.println ("No.:" + s.ID + "\ t\ t\ t name:" + s.name + "\ t\ t\ t Age:" + s.age + "\ t\ t gender:" + s.gender);}}

From a code point of view, the foreach statement is obviously much simpler than the for loop statement, without the need to define initial values, determine the scope of judgment, and do not need to increase or decrease. But this does not mean that foreach statements can replace for loop statements. Obviously, what foreach can do, for loops can do; what for loops can do, foreach can not necessarily do.

3. Iterable interface

Iterate through the elements in the collection through the iterator Iterable:

System.out.println ("(iterator) student information:"); Iterator it=students.iterator (); while (it.hasNext ()) {Student s = (Student) it.next (); System.out.println ("No.:" + s.ID + "\ t\ t\ t name:" + s.name + "\ t\ t\ t Age:" + s.age + "\ t\ t gender:" + s.gender) }

The iterator is used to traverse the elements in the collection, which itself does not have the storage capacity and is dependent on the collection.

Thank you for your reading, the above is the "Java collection traversing all the elements of the method of what" the content, after the study of this article, I believe you have a deeper understanding of the method of traversing all elements of the Java collection, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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