当前位置:网站首页>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());
}
}
}
边栏推荐
- Advantages of win8.1 and win10
- Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution論文淺析
- 2021tami/ image processing: exploiting deep generative priority for versatile image restoration and manipulation
- PostgreSQL Guide: Insider exploration (Chapter 7 heap tuples and index only scanning) - Notes
- Std:: Map initialization
- String()和toString()方法得区别
- metaRTC4.0稳定版发布
- Clause 31: avoid default capture mode
- 关于匿名内部类
- C language learning log 10.11
猜你喜欢
System file interface open
Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions
Explain the differences and usage scenarios between created and mounted
Section 8 - Practical commissioning techniques
Luogu p1036 number selection
Understanding inode
Spread your wings and soar
C language learning log 1.22
Section 5 - Operator details
Binary search and binary answer
随机推荐
135. distribute candy
Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions
metaRTC4.0稳定版发布
QT realizes message sending and file transmission between client and server
Time display of the 12th Blue Bridge Cup
UNO
Sort (internal sort) + external sort
Win8.1和Win10各自的優勢
C language learning log 12.5
Sampo Lock
Luogu p1036 number selection
OpenCV中的saturate操作(饱和操作)究竟是怎么回事
Leetcode game 297 (20220612)
Search DFS and BFS
Analysis of the principle of V-model and its application in user defined components
Chapter 17 free space management
详解OpenCV的函数cv::add(),并附各种情况的示例代码和运行结果
Infinite cycle scrolling code Alibaba international station store decoration code base map scrolling black translucent display effect custom content decoration code full screen display
Chapter 14 introduction: memory operation API
External sort