集合的遍历

这里写图片描述

集合的长度是可变的:
1.Collection 接口:子类接口分别是List和Set
List:常用的实类,ArrayList ,LinkedList.
ArrayList:是基于数组,特点是查询快,增删慢,效率高安全性低。
LinkedList:是基于链表,增删快,查询慢。
Vector:也是基于数组,和ArrayList的区别,安全性高,效率低
2.Map得子类:键值对遍历
HashMap
LinkedHashMap

`list的遍历:
public class MyLinkedList {
    List<Shape> list; 

    public void genDate(){
        list=new LinkedList<Shape>();
        list.add(new DrawLine(1,1,1,1,1));
        list.add(new DrawLine(2,1,1,1,1));
        list.add(new DrawLine(3,1,1,1,1));
        list.add(new DrawLine(4,1,1,1,1));
        list.add(new DrawLine(5,1,1,1,1));

    }

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

Map遍历:public class MyMap {

public class MyMap {

    Map<Integer,Shape> myMap = new HashMap<Integer,Shape>();;

    public void genDate() {
        myMap.put(1, new DrawLine(1, 1, 1, 1, 1));
        myMap.put(2, new DrawLine(2, 1, 1, 1, 1));
        myMap.put(3, new DrawLine(3, 1, 1, 1, 1));
        myMap.put(4, new DrawLine(4, 1, 1, 1, 1));
    }

    public void printDate() {
        Set<Integer> set = myMap.keySet();
        Iterator<Integer> it = set.iterator();
        while(it.hasNext()){
            int key = it.next();
            System.out.println(myMap.get(key));
        }
    }
}

Map遍历的第二种方法效率更高

HashMap<String, Shape> hashMap = new HashMap<String, Shape>();

        hashMap.put("shape1", new DrawLine(1, 1, 1, 1, null, 1));
        hashMap.put("shape2", new DrawLine(2, 1, 1, 1, null, 1));
        hashMap.put("shape3", new DrawLine(3, 1, 1, 1, null, 1));
        hashMap.put("shape4", new DrawLine(4, 1, 1, 1, null, 1));
        hashMap.get("shape1");
        Set<Entry<String,Shape>> set = hashMap.entrySet();
        Iterator<Entry<String ,Shape>> it = set.iterator();
        while(it.hasNext()){
            Entry<String,Shape> en = (Entry<String,Shape>)it.next();
            String key = en.getKey();
            String value = en.getValue();
            System.out.println(key+"======"+value);
        }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值