标签:false code lse void 个学生 增强for循环 变量 通过 private
package com.peihua.cn.test1.Test; public class Student { private String name; private int age; private String Xxingbie; public Student() { } public Student(String name, int age, String xxingbie) { this.name = name; this.age = age; Xxingbie = xxingbie; } 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; } public String getXxingbie() { return Xxingbie; } public void setXxingbie(String xxingbie) { Xxingbie = xxingbie; } }
package com.peihua.cn.test1.Test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ListTest { public static void main(String[] args) { // list集合继承自collection,arraylist属于它的子类 // 创建list集合, List<Student> list = new ArrayList<Student>(); //创建学生对象 Student s1 = new Student("小燕子", 18, "女"); Student s2 = new Student("小李子", 19, "男"); Student s3 = new Student("小猴子", 18, "男"); //使用list集合的add方法,将变量值添加到list集合,list集合有序排列 list.add(s1); list.add(s2); list.add(s3); //迭代器遍历,创建迭代器对象 Iterator<Student> iterable = list.iterator(); // 将迭代器的方法hasNext的结果值true或false作为判断条件 // 再通过迭代器的方法next,进行依次有序寻找,直到hasNext的结果是false,遍历结束 System.out.println("迭代器遍历循环方式:"); while (iterable.hasNext()) { //创建学生对象用来接受迭代器的元素 Student student = iterable.next(); //打印输出 System.out.println(student.getName() + " " + student.getAge() + " " + student.getXxingbie()); } // 也可以使用for循环方式,因为在list集合中有get方法,取出元素 System.out.println("for循环遍历方式:"); for (int i = 0; i < list.size(); i++) { //创建学生对象,通过get方法获取到元素,赋值给学生对象的变量 Student student = list.get(i); //打印输出 System.out.println(student.getName() + " " + student.getAge() + " " + student.getXxingbie()); } System.out.println("增强for循环遍历方式:"); //增强for循环,是lsit集合特有的方式,其内部还是个迭代器 //格式 for(元素数据类型 变量名 : 数组/集合对象名){} for (Student student : list) { System.out.println(student.getName() + " " + student.getAge() + " " + student.getXxingbie()); } } }
标签:false code lse void 个学生 增强for循环 变量 通过 private
原文地址:https://www.cnblogs.com/521521cm/p/14349265.html