跟王老師學集合(三):使用Iterator接口遍歷集合元素

使用Iterator接口遍歷集合元素

主講人:王少華  QQ羣號:483773664

學習目標:

1 掌握使用Iterator接口遍歷集合元素javascript

1、Iterator概述

Iterator接口主要用於遍歷,所以Iterator對象也被稱爲迭代器。Iterator接口裏定義了以下三個方法:
php

  • boolean hasNext():若是被迭代的集合元素尚未被遍歷,則返回truecss

  • Object next():返回集合裏下一個元素html

  • void remove():刪除集合裏上一次next方法返回的元素前端

2、利用Iterator來遍歷寵物集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Test {
     public static void main(String[] args) {
         // 一、建立四個狗狗對象
         Dog ououDog = new Dog( "歐歐" , "雪娜瑞" );
         Dog yayaDog = new Dog( "亞亞" , "拉布拉多" );
         Dog ×××Dog = new Dog( "美美" , "雪娜瑞" );
         Dog feifeiDog = new Dog( "菲菲" , "拉布拉多" );
         // 二、建立ArrayList集合對象並把四個狗狗對象放入其中
         List dogs = new ArrayList();
         dogs.add(ououDog);
         dogs.add(yayaDog);
         dogs.add(×××Dog);
         dogs.add( 2 , feifeiDog); // 添加feifeiDog到指定位置
         // 三、輸出集合中狗狗的數量
         System.out.println( "共計有" + dogs.size() + "條狗狗。" );
         // 四、經過遍歷集合顯示各條狗狗信息
         //4.1 得到集合中的迭代器
©著做權歸做者全部:來自51CTO博客做者編程魔法師的原創做品,如需轉載,請註明出處,不然將追究法律責任

0java

收藏webpack

編程魔法師

101篇文章,15W+人氣,0粉絲

Ctrl+Enter 發佈git

發佈web

取消

掃一掃,領取大禮包

0

分享
編程魔法師
相關文章
相關標籤/搜索