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

package com.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.anli;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Demo {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student s1 = new Student("1", 21);
Student s2 = new Student("2", 22);
Student s3 = new Student("3", 23);
list.add(s1);
list.add(s2);
list.add(s3);
// A collection of traverse
//① Iterator traversal
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
Student ss = it.next();
System.out.println(ss.getName() + "," + ss.getAge());
}
//② Ordinary for Traversal with index
for (int i = 0; i < list.size(); i++) {
Student sss = list.get(i);
System.out.println(sss.getName() + "," + sss.getAge());
}
// enhance for
for (Student stu : list) {
System.out.println(stu.getName() + "," + stu.getAge());
}
}
}

边栏推荐
猜你喜欢

Draw a hammer

Shell built-in string substitution

BM1Z002FJ-EVK-001开机测评

PostgreSQL Guide: inside exploration (Chapter 10 basic backup and point in time recovery) - Notes

Ruoyi cloud startup tutorial (hand-held graphics)

Article 29: assuming that the mobile operation does not exist, is expensive, and is not used

Spread your wings and soar

Section 4 - arrays

Stepping on a horse (one stroke)

External sort
随机推荐
Section 7 - structures
Redis plus instructions
priority inversion problem
Spread your wings and soar
QT realizes message sending and file transmission between client and server
Luogu p1036 number selection
Time display of the 12th Blue Bridge Cup
metaRTC4.0稳定版发布
Analysis of the principle of V-model and its application in user defined components
std::condition_ variable::wait_ for
RuoYi-Cloud启动教程(手把手图文)
Article 49: understanding the behavior of new handler
PostgreSQL Guide: Insider exploration (Chapter 7 heap tuples and index only scanning) - Notes
Avantages de win8.1 et win10
Listiterator list iterator
RTSP streaming using easydarwin+ffmpeg
Explain the differences and usage scenarios between created and mounted
Leetcode game 297 (20220612)
Section 3 - functions
Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases