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

边栏推荐
猜你喜欢

QT interface rendering style

How to lay copper in AD (aluminum designer)

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

Advanced C - Section 2 - pointers

priority inversion problem

Explain the role of key attribute in V-for

External sort

C language learning log 12.5

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

Kaggle 时间序列教程
随机推荐
Mysql8.0.13 installation tutorial (with pictures)
BM1Z002FJ-EVK-001开机测评
【转载】C语言内存和字符操作函数大全
COAP protocol libcoap API
RTSP streaming using easydarwin+ffmpeg
QT using layout manager is invalid or abnormal
Article 29: assuming that the mobile operation does not exist, is expensive, and is not used
Dup2 use
Interpretation of QT keypressevent
Modification and analysis of libcoap source code by Hongmeng device discovery module
Clause 26: avoid overloading universal reference types
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution論文淺析
[leetcode]- sliding window
Chapter 18 pagination: Introduction
Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases
Kaggle time series tutorial
Kaggle 时间序列教程
Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis
Logical point
Red Treasure Book Reading Notes (continuously updated)