zoukankan      html  css  js  c++  java
  • java集合List的遍历方式

       循序渐进学习java 集合的遍历方式:

      一、先以list集合为例:

      package com.test;

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

    public class testing {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            List<String> list=new ArrayList<String>();
            list.add("a");
            list.add("b");
            list.add("c");
            list.add("d");
            list.add("e");
            
            //方法一:(迭代器(1))
            for(Iterator<String> iterator=list.iterator();iterator.hasNext();){
                String st=iterator.next();
                
                System.out.println("遍历方法一的list的输出的String 是"+st);
            }
            
            //方法二:for循环(1)
            for(int i=0;i<list.size();i++){
                String st=list.get(i);
                System.out.println("遍历方法二的list的输出的String ="+st);
            }
            
            
            //方法三:for循环(2)
            for(String st:list){
                System.out.println("遍历方法三的list输出的String 为 "+st);
            }
            
            //方法四 :(迭代器(2))
            Iterator<String> iterator=list.iterator();
            while(iterator.hasNext()){
                String st=iterator.next();
                System.out.println("遍历方法四list输出的String is "+st);
                
            }
        }

    }

    输出如下:

    遍历方法一的list的输出的String 是a
    遍历方法一的list的输出的String 是b
    遍历方法一的list的输出的String 是c
    遍历方法一的list的输出的String 是d
    遍历方法一的list的输出的String 是e
    遍历方法二的list的输出的String =a
    遍历方法二的list的输出的String =b
    遍历方法二的list的输出的String =c
    遍历方法二的list的输出的String =d
    遍历方法二的list的输出的String =e
    遍历方法三的list输出的String 为 a
    遍历方法三的list输出的String 为 b
    遍历方法三的list输出的String 为 c
    遍历方法三的list输出的String 为 d
    遍历方法三的list输出的String 为 e
    遍历方法四list输出的String is a
    遍历方法四list输出的String is b
    遍历方法四list输出的String is c
    遍历方法四list输出的String is d
    遍历方法四list输出的String is e

    !============记录点滴成长============!

    !============记录点滴成长============!
  • 相关阅读:
    SurvivalShooter学习笔记(二.玩家移动旋转)
    SurvivalShooter学习笔记(一.相机跟随)
    Unity平台的预处理
    设计模式之简单工厂模式
    求每一位数和
    10进制转化为m进制
    m进制转化为10进制
    【硬件模块】华为NBIOT 使用记录
    【编程语言】Matlab 学习记录
    【设计模式】观察者模式
  • 原文地址:https://www.cnblogs.com/dark-passion/p/5039404.html
Copyright © 2011-2022 走看看