A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© KID灬熊孩子 中级黑马   /  2014-8-18 21:38  /  1198 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

用增强for循环 for(List l:list)
用迭代器  Iterator
get()和size()方法结合实现

6 个回复

倒序浏览
建议写个代码,这样的话更直观
回复 使用道具 举报
对啊,么错,LZ想问啥...
回复 使用道具 举报
  1. package com.itheima;

  2. import java.util.*;

  3. public class ForeachDemo {

  4.         public static void main(String[] args)
  5.         {
  6.                 ArrayList<String> al= new ArrayList<String>();
  7.                 al.add("aaa");
  8.                 al.add("bbb");
  9.                 al.add("ccc");
  10.                 al.add("bbb");

  11.                 //迭代器
  12.                 Iterator it= al.iterator();
  13.                 while(it.hasNext())
  14.                 {
  15.                         System.out.println("Itrator::"+it.next());
  16.                 }
  17.                
  18.                 //foreach
  19.                 for(String str : al)
  20.                 {
  21.                         System.out.println("str::"+str);
  22.                 }
  23.                
  24.                 //for循环get()方法
  25.                 for(int x=0; x<al.size(); x++)
  26.                 {
  27.                         System.out.println("al("+x+")=="+al.get(x));
  28.                 }
  29.         }

  30. }
复制代码
回复 使用道具 举报
再详细些,把问题描述清楚
回复 使用道具 举报
不知道什么情况
回复 使用道具 举报
肖明 来自手机 中级黑马 2014-8-18 22:49:14
7#
总结的对,三种,第三个是list特有的遍厉吧
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马