当前位置:网站首页>Case - the ArrayList collection stores student objects and traverses them in three ways
Case - the ArrayList collection stores student objects and traverses them in three ways
2022-06-13 05:05:00 【Jason_ LH1024】

package com.it.anli;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.it.anli;
import java.util.ArrayList;
import java.util.Iterator;
public class Demo {
public static void main(String[] args) {
ArrayList<Student> array = new ArrayList<>();
Student s1 = new Student(" eldest brother ", 22);
Student s2 = new Student(" Second brother ", 21);
Student s3 = new Student(" Three elder brother ", 20);
array.add(s1);
array.add(s2);
array.add(s3);
//1. Iterator traversal
Iterator<Student> it = array.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName() + "," + s.getAge());
}
System.out.println("================================");
//2. Ordinary for
for (int i = 0; i < array.size(); i++) {
Student ss = array.get(i);
System.out.println(ss.getName() + "," + ss.getAge());
}
System.out.println("================================");
//3. enhance for
for (Student sss:array) {
System.out.println(sss.getName() + "," + sss.getAge());
}
}
}

边栏推荐
- Implementing the driver registration initcall mechanism on stm32
- [multi thread programming] the future interface obtains thread execution result data
- Clause 26: avoid overloading universal reference types
- Section 2 - branch and loop statements
- Chapter 17 free space management
- Section 8 - Practical commissioning techniques
- C language learning log 10.4
- Time display of the 12th Blue Bridge Cup
- Std:: Map initialization
- On switch() case statement in C language
猜你喜欢

Advanced C - Section 2 - pointers

2021TPAMI/图像处理:Exploiting Deep Generative Prior for Versatile Image Restoration and Manipulation

Analysis on the similarities and differences of MVC, MVP and mvvc

Embedded hardware: electronic components (1) resistance capacitance inductance

Section 7 - structures

Solution to sudden font change in word document editing

Robot pose description and coordinate transformation

C language learning log 12.5

关于匿名内部类

About mission planning and improving execution
随机推荐
Win8.1和Win10各自的優勢
QT signal is automatically associated with the slot
Regular expressions in QT
List collection concurrent modification exception
C language learning log 10.11
Section 8 - Practical commissioning techniques
Spice story
Advanced C - Section 3 - character functions and string functions
C language learning log 1.17
Std:: Map empty example
lookup
Sort (internal sort) + external sort
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution論文淺析
Luogu p1088 Martians
OJ problem solution summary
QT brushes and brushes
On switch() case statement in C language
C language learning log 10.4
Chapter 17 free space management
priority inversion problem