• 三种方式遍历ArrayList


    package com.Test01;

    import java.util.ArrayList;
    import java.util.Iterator;

    public class ArrayListDemo {
    public static void main(String[] args) {
    //创建ArrayList集合对象
    ArrayList<Student> array = new ArrayList<Student>();

    //创建学生对象 提前定义学生类
    Student s1 = new Student("王五",20);
    Student s2 = new Student("张五",21);
    Student s3 = new Student("李五",22);

    //添加学生对象到集合中

    array.add(s1);array.add(s2);array.add(s3);

    //迭代器遍历集合:集合特有的遍历方式
    Iterator<Student> it = array.iterator();
    while(it.hasNext()) {
    Student s = it.next();
    System.out.println(s.getName()+","+s.getAge());
    }
    System.out.println("------------------------");

    //普通for遍历,带有索引
    for(int i = 0;i<array.size();i++) {
    Student s = array.get(i);
    System.out.println(s.getName()+","+s.getAge());
    }
    System.out.println("-----------------------");

    //增强for遍历
    for(Student s : array) {
    System.out.println(s.getName()+","+s.getAge());
    }

    }
    }
  • 相关阅读:
    服务器端口
    Format(const wchar_t *,...)”: 不能将参数 1 从“const char [3]”转换为“const wchar_t *”.
    图片格式
    CreateEx
    电力谐波
    [OGeek2019]babyrop
    Simple Inject
    [GXYCTF2019]BabySQli
    [CISCN2019 华北赛区 Day2 Web1]Hack World
    极客大挑战2019
  • 原文地址:https://www.cnblogs.com/lsswudi/p/11407915.html
Copyright © 2020-2023  润新知