Collection中的设计模式

迭代器模式

调用iterator方法,获取迭代器Iterator

 

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class TestCollection {
    public static void main(String[] args) {
     Collection pers=new ArrayList();
     pers.add(new Person("jarvis",18));
     pers.add(new Person("robin",18));
     pers.add(new Person("wzb",18));

        Iterator iterator=pers.iterator();

// 判断,如果返回true,则进行下一步
// Way1
        while (true) {
            if (iterator.hasNext()) {
                System.out.println("Way1" + iterator.next());
            } else {
                break;
            }
        }
//Way 2
        while (iterator.hasNext()) {
            System.out.println("Way2" + iterator.next());
        }

        //为了简化Iterator的语法,jdk5.0出现增强for,本质上就是Iterator,只是语法简化了
//Way3
        for (Object o : pers) {
            System.out.println("Way3" + o);
        }

    }
}

foreach 方法来遍历

从 JDK 1.5 之后可以使用 foreach 方法来遍历实现了 Iterable 接口的聚合对象

for(元素类型 元素名:集合或数组名){

}

问题:Way2没有输出,为什么呢?

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class TestCollection {
    public static void main(String[] args) {
        Collection pers = new ArrayList();
        pers.add(new Person("jarvis", 18));
        pers.add(new Person("robin", 18));
        pers.add(new Person("wzb", 18));

        Iterator iterator = pers.iterator();


        while (iterator.hasNext()) {
            pers.add(new Person("xiaoyu",17));
            System.out.println(iterator.next());
        }

    }
}

 

报错:java.util.ConcurrentModificationException  非同步的更新异常

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class TestCollection {
    public static void main(String[] args) {
        Collection pers = new ArrayList();
        pers.add(new Person("jarvis", 20));
        pers.add(new Person("robin", 18));
        pers.add(new Person("wzb", 21));

        Iterator iterator = pers.iterator();


        while (iterator.hasNext()) {
            Person next=(Person)iterator.next();
            if(next.age<19){
                pers.remove(next);
            }
            System.out.println(iterator.next());

        }

    }
}

 

还是错,这次是NoSuchElementException

 

每次都会去比较记录的实际的个数

为了避免出现信息不安全,影响自己本身的功能,设立了一个变量,记录实际有几个

 

使用迭代器过程,不适合做增删,容易报异常ConCurrentModificationException

使用迭代器过程。可以做修改,但如果修改地址,没有效果

使用迭代器过程,如非要删除,可以使用迭代器本身的remove方法

 

适配器模式

java.util.Arrays#asList() 可以把数组类型转换为 List 类型。

应该注意的是 asList() 的参数为泛型的变长参数,不能使用基本类型数组作为参数,只能使用相应的包装类型数组。

import java.util.Arrays;
import java.util.List;

public class dasdsa {
    public static void main(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student();
        students[1]=new Student();
        students[2]=new Student();
        students[0].setAge(20);
        System.out.println("students[0] 的年纪 = " + students[0].age);
        System.out.println("students数组的长度 = " + students.length);
    /*
    复制数组
     */
        //先新建一个数组
        Student[] newstudents=new Student[students.length+1];
        //再复制过去
        for (int i = 0; i <students.length ; i++) {
            newstudents[i]=students[i];
        }
        //也可使用arraycopy进行复制
        //System.arraycopy(students,0,newstudents,0,3);
        System.out.println("newstudents[0] 的年纪 = " + newstudents[0].age);
        System.out.println("newstudents数组的长度 = " + newstudents.length);



        List list= Arrays.asList(newstudents);

        System.out.println("list = " + list);
        System.out.println("list size = " + list.size());

        for (int i = 0; i <list.size() ; i++) {
            System.out.println(list.get(i).toString());
        }


    }


}

class Student {
    String name;
    int age;
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

students数组转为List成功,但是newstudents数组转化失败

 

也可以使用以下方式调用 asList():

import java.util.Arrays;
import java.util.List;

public class dasdsa {
    public static void main(String[] args) {
        List list = Arrays.asList(1, 2, 3);
        System.out.println("list = " + list);

    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值