当前位置:网站首页>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());
}
}
}
边栏推荐
- Mind mapping series - Database
- JS to realize the conversion between string and array and an interview question
- Stepping on a horse (one stroke)
- 【转载】C语言内存和字符操作函数大全
- Chapter 13 abstraction: address space
- Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution论文浅析
- OJ problem solution summary
- C language learning log 12.14
- PostgreSQL Guide: inside exploration (Chapter 10 basic backup and point in time recovery) - Notes
- Embedded hardware - read schematic
猜你喜欢
Robot pose description and coordinate transformation
Bm1z002fj-evk-001 startup evaluation
priority inversion problem
Section 7 - structures
metaRTC4.0集成ffmpeg编译
BM1Z002FJ-EVK-001开机测评
Advanced C language - Section 1 - data storage
Embedded hardware: electronic components (1) resistance capacitance inductance
Sampo Lock
QT client development -- driver loading problem of connecting to MySQL database
随机推荐
Dynamic and static libraries
How to understand JS expressions and JS statements
C language learning log 10.4
Chapter 17 free space management
C language learning log 10.5
Chapter 18 pagination: Introduction
Analysis on the similarities and differences of MVC, MVP and mvvc
Force deduction 121 questions
Solution to sudden font change in word document editing
Chapter 14 introduction: memory operation API
Flex布局自适应失效的问题
Shell variable learning notes
metaRTC4.0稳定版发布
C language learning log 1.16
[multithreading] thread pool core class -threadpoolexecutor
Hidden implementation and decoupling, knowing Pimpl mode
Article 29: assuming that the mobile operation does not exist, is expensive, and is not used
Sort (internal sort) + external sort
josephus problem
OJ problem solution summary