当前位置:网站首页>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());
}
}
}

边栏推荐
- metaRTC4.0集成ffmpeg编译
- JS to realize the conversion between string and array and an interview question
- 2021tami/ image processing: exploiting deep generative priority for versatile image restoration and manipulation
- C language learning log 10.10
- Draw a hammer
- Section 2 - branch and loop statements
- Std:: Map initialization
- Luogu p3654 fisrt step
- Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution論文淺析
- Time display of the 12th Blue Bridge Cup
猜你喜欢
随机推荐
Implementing the driver registration initcall mechanism on stm32
【多线程】线程池核心类-ThreadPoolExecutor
135. distribute candy
Clause 31: avoid default capture mode
Sort (internal sort) + external sort
Advanced C - Section 2 - pointers
std::condition_ variable::wait_ for
RT thread console device initialization
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution论文浅析
Section 4 - arrays
[leetcode]- sliding window
Clause 27: alternatives to overloading with familiar universal reference types
Redis plus instructions
System file interface open
Recursion and recursion
About mission planning and improving execution
C language learning log 1.2
Section 8 - Practical commissioning techniques
Clause 48: understand template metaprogramming
Dynamic and static libraries









