• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

List、Set、Map、Queue、Deque、Stack的遍历方法

武飞扬头像
爱喝冰红茶阿
帮助2

文章目录


一、遍历List

1,使用get(int)方法遍历,由于List的行为和数组几乎完全相同:List内部存放元素是按照元素的先后顺序进行顺序存放的,每个元素都可以通过索引确定自己的位置,List的索引和数组一样,从0开始

  1.  
    public class Main {
  2.  
        public static void main(String[] args) {
  3.  
            List<String> list =  Arrays.asList("杭州", "北京", "上海", "南京");
  4.  
            for (int i=0; i<list.size(); i ) {
  5.  
                String s = list.get(i);
  6.  
                System.out.println(s);
  7.  
            }
  8.  
        }
  9.  
    }

2、使用迭代器iterator()遍历,Iterator本身是一个对象,但它是由List的实例调用iterator()方法的时候创建的。Iterator对象知道如何遍历一个List,并且不同的List类型,返回的Iterator对象也是不同的,但总是有最高的访问效率。

boolean hasnext() 判断是否由下一个元素     E next()返回下一个元素

  1.  
    public class Main {
  2.  
        public static void main(String[] args) {
  3.  
            List<String> list = Arrays.asList("杭州", "北京", "上海", "南京");
  4.  
            for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
  5.  
                String s = it.next();
  6.  
                System.out.println(s);
  7.  
            }
  8.  
        }
  9.  
    }

二、遍历Set

  Set是Collection下面的无序子接口

  特点:无序、没有索引、不能重复

1、使用toArray(),转换成一个数组,对数组进行遍历;

  1.  
    public class Main {
  2.  
        public static void main(String[] args) {
  3.  
            Set<String> set = new HashSet<String>();
  4.  
            Object[ ] objects = set.toArray();
  5.  
            for(int i = 0; i < objects.length; i ){
  6.  
                  System.out.println(objects[i] "");
  7.  
            }
  8.  
      }
  9.  
    }

2、使用迭代器iterator()遍历

  1.  
    public class Main {
  2.  
        public static void main(String[] args) {
  3.  
            Set<String> set = new HashSet<String>();
  4.  
              for (Iterator<String> it = set.iterator(); it.hasNext(); ) {
  5.  
                   String s = it.next();
  6.  
                System.out.println(s);
  7.  
            }
  8.  
      }
  9.  
    }

3、使用foeach循环进行遍历

  1.  
    public class Main {
  2.  
        public static void main(String[] args) {
  3.  
            Set<String> set = new HashSet<String>();
  4.  
            for (String s : set){
  5.  
                System.out.println(s);
  6.  
            }
  7.  
       }
  8.  
    }

三、遍历Map

1、使用foreach循环遍历Map实例的keySet()方法返回的Set集合

  1.  
    public class Main {
  2.  
        public static void main(String[] args) {
  3.  
            Map<String,Integer> map = new HashMap<>();
  4.  
            for (String key : map.keySet()){
  5.  
                    Integer value = map.get(key);
  6.  
                    System.out.println(key "=" value);
  7.  
            }
  8.  
       }
  9.  
    }

2、遍历使用foreach循环遍历Map对象的entrySet()集合,它包含每一个key-value的映射

  1.  
    public class Main {
  2.  
        public static void main(String[] args) {
  3.  
            Map<String,Integer> map= new HashMap<>();
  4.  
            for(Map.Entry<String,Integer> entry : map.entrySet())){
  5.  
                          String key = entry.getKey();
  6.  
                          Integer value = entry.getValue();
  7.  
                          System.out.println(key "=" value);
  8.  
            }
  9.  
       }
  10.  
    }

四、遍历Queue

1、使用foreach循环遍历queue

  1.  
    public class Main {
  2.  
        public static void main(String[] args) {
  3.  
            Queue<String> queue = new LinkedList<String>();
  4.  
            for(String temp:queue){
  5.  
    System.out.println(temp);
  6.  
    }
  7.  
      }
  8.  
    }

2、使用迭代器iterator()遍历

  1.  
     
  2.  
    public class Main {
  3.  
        public static void main(String[] args) {
  4.  
            Queue<String> queue = new LinkedList<String>();
  5.  
    Iterator it = queue.iterator();
  6.  
            while(it.hasnext()){
  7.  
    System.out.println(it.next);
  8.  
    }
  9.  
      }
  10.  
    }

3、使用队列Queue的自带方法

boolean isEmpty() 判断队列中元素是否为空

E poll() 取出队首元素并删除

  1.  
     
  2.  
    public class Main {
  3.  
        public static void main(String[] args) {
  4.  
            Queue<String> queue = new LinkedList<String>();
  5.  
            while(!queue.isEmpty()){
  6.  
    System.out.println(queue.poll());
  7.  
    }
  8.  
      }
  9.  
    }

五、遍历Deque

1、foreach循环遍历

  1.  
    public class Main {
  2.  
        public static void main(String[] args) {
  3.  
            Deque<String> deque = new LinkedList<String>();
  4.  
            for (String string : deque) {
  5.  
    System.out.println(string);
  6.  
    }
  7.  
      }
  8.  
    }

2、使用迭代器遍历

  1.  
    public class Main {
  2.  
        public static void main(String[] args) {
  3.  
            Deque<String> deque = new LinkedList<String>();
  4.  
            Iterator<String> it = deque.iterator();
  5.  
    while(it.hasNext()) {
  6.  
    System.out.println(it.next());
  7.  
    }
  8.  
      }
  9.  
    }

六、遍历stack

1、while循环遍历

  1.  
  2.  
    public class Main {
  3.  
        public static void main(String[] args) {
  4.  
            Stack<String> stack = new Stack<String>();
  5.  
    while(stack.isEmpty()) {
  6.  
    System.out.println(stack.pop());
  7.  
    }
  8.  
      }
  9.  
    }
  10.  
     
  11.  

2、foreach循环遍历

  1.  
  2.  
    public class Main {
  3.  
        public static void main(String[] args) {
  4.  
            Stack<String> stack = new Stack<String>();
  5.  
            Iterator<String> it = stack.iterator();
  6.  
    while(it.hasNext()) {
  7.  
    System.out.println(it.next());
  8.  
    }
  9.  
      }
  10.  
    }
  11.  
     
  12.  

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcfecj
系列文章
更多 icon
同类精品
更多 icon
继续加载