java集合List的遍历方式

it2022-05-05  58

   循序渐进学习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

 

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

 

转载于:https://www.cnblogs.com/dark-passion/p/5039404.html

相关资源:java中List对象集合的遍历方法(三个)

最新回复(0)