一、遍历方式

  1. 迭代器
  2. 增强for循环
  3. 普通for循环

二、使用

迭代器:
public class IteratorMethod {
public static void main(String[] args) {
Collection<String> Coll=new ArrayList<>();
Coll.add("姚明");
Coll.add("科比");
Coll.add("麦迪");
Coll.add("艾弗森");
Coll.add("欧文");
System.out.println(Coll);
System.out.println("=================");
//多态 接口 实现类对象
Iterator<String> it=Coll.iterator(); while(it.hasNext()){
String s=it.next();
System.out.println(s);
}
}
}
增强for循环:
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;
}
}
......{
List<Student> students = new ArrayList<>();
students.add(new Student("科比",24));
students.add(new Student("麦迪",13));
students.add(new Student("乔丹",64));
students.add(new Student("姚明",20)); for(Student st : stufents) {
System.out.println(st);
}
}
普通for循环:
......
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println(s);
}
......

Java 遍历方式的更多相关文章

  1. java遍历map的四种方式

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  2. Java Map遍历方式的选择

    [原文] 1. 阐述 对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多.理由是:entrySet方法一次拿到所有key和value的集合:而keyS ...

  3. Java Map各遍历方式的性能比较

    1. 阐述 对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多.理由是:entrySet方法一次拿到所有key和value的集合:而keySet拿到的 ...

  4. Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]

    Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 @author Trinea 原文链接:http://www.trinea.cn/android/arrayl ...

  5. Java中List集合的三种遍历方式(全网最详)

    List集合在Java日常开发中是必不可少的,只要懂得运用各种各样的方法就可以大大提高我们开发的效率,适当活用各种方法才会使我们开发事半功倍. 我总结了三种List集合的遍历方式,下面一一来介绍. 首 ...

  6. Java遍历Map对象的四种方式

    关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 :这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> m ...

  7. 【转】Java遍历Map对象的四种方式

    关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> ma ...

  8. java 遍历Map的四种方式

      java 遍历Map的四种方式 CreationTime--2018年7月16日16点15分 Author:Marydon 一.迭代key&value 第一种方式:迭代entrySet 1 ...

  9. Java LinkList遍历方式

    1.LinkedList的遍历方式 a.一般的for循环(随机访问) int size = list.size(); for (int i=0; i<size; i++) { list.get( ...

  10. 【Java】集合遍历--List和Map的多种遍历方式

    1. List的两种遍历方式 package com.nova.test; import java.util.ArrayList; import java.util.Iterator; import ...

随机推荐

  1. 定长线程池Demo

    1 import java.util.concurrent.ExecutorService; 2 import java.util.concurrent.Executors; 3 4 /** 5 * ...

  2. QT搭建Ffmpeg开发环境gcc版本

    1 先安装qt 解压ffmpeg包 2打开qt创建工程 3 导入头文件和库文件 这里一定要注意gcc版本和库的版本一定要一致 4 添加一下简单的源代码 1 #include <libavcode ...

  3. DER编码

    1. Attribute Type编码 ​ 对于标识串,采用低标识编码方式,只需1个字节.OBJECT IDENTIFIER的 tag为Ox06;class选择universal,则位8和位7为0,O ...

  4. Linux 同步分发脚本

    shell 内容 #!/bin/bash #1. 判断参数个数 if [ $# -lt 1 ] then echo Not Enough Arguement! exit; fi 尚硅谷大数据技术之 H ...

  5. vue3.0+vite按需引入element plus

    1.安装vite-plugin-style-import yarn add vite-plugin-style-import -D 2.在项目根目录下的vite.config.js中配置 import ...

  6. 打开CMD的方式

    打开Cmd的方式: 1.开始+系统+命令提示符 2.Win键+R 3.在任意文件夹下,按住Shift键+鼠标右键点击,在此处打开命令行窗口 4.资源管理的地址栏前面加上Cmd路径 管理员运行方式 :系 ...

  7. 重写antd组件样式

    :global { .ant-select-selection-placeholder { color: #FFF; font-size: 14px; } .ant-select-selection- ...

  8. LINUX下的VSCODE-C/C++配置

    LINUX下的VSCODE-C/C++配置 1.生成默认的任务文件 2.lunch.json,调整"configurations"里的成员,如下 ①添加 "preLaun ...

  9. 离线安装docker和harbor

    1.下载docker和harbor版本(版本自选) docker下载地址: https://download.docker.com/linux/static/stable/x86_64/ harbor ...

  10. thread互斥测试

    thread互斥测试 实践代码 #include <stdio.h> #include <stdlib.h> #include <pthread.h> //linu ...