JAVA集合遍历

一.Set

public class text {
    public static void main(String[] args) {
        Set<String> a = new HashSet<String>();
        a.add("aa");
        a.add("bb");
        a.add("cc");
        a.add("dd");
        a.add("ee");
        a.add("ff");
        // 1.使用foreach遍历
        for (Object i : a) {
            System.out.println(i);
        }
        // 2.使用迭代器iterator遍历
        // (1)遍历
        Iterator<String> it = a.iterator();
         while (it.hasNext()) {
         System.out.println(it.next());
         }
        // (2)移除某个元素
        Iterator<String> it2 = a.iterator();
        while (it2.hasNext()) {
            if (it2.next().equals("aa")) {
                it2.remove();
            }
        }
        for (Object i : a) {
            System.out.println(i);
        }
    }
}

二.List

public class text2 {

public static void main(String[] args) {
  List<String> a = new ArrayList<String>();
  a.add("aa");
  a.add("bb");
  a.add("cc");
  a.add("dd");
  a.add("ee");
  a.add("ff");
  // 1.使用foreach遍历
  for (Object i : a) {
   System.out.println(i);
  }
  // 2.使用迭代器iterator遍历
  // (1)遍历
  Iterator<String> it = a.iterator();
  while (it.hasNext()) {
   System.out.println(it.next());
  }
  // (2)移除某个元素
  Iterator<String> it2 = a.iterator();
  while (it2.hasNext()) {
   if (it2.next().equals("aa")) {
    it2.remove();
   }
  }
  for (Object i : a) {
   System.out.println(i);
  }

}

}

时间: 2024-08-08 15:15:31

JAVA集合遍历的相关文章

java 集合遍历时删除元素

本文探讨集合在遍历时删除其中元素的一些注意事项,代码如下 ? 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 import java.util.ArrayList; import java.util.Iterator; import java

Java集合遍历时删除

public static void main(String[] args){ List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); Iterator<Integer> interator = list.iterator(); while(interator.hasNext()){ Integer i

Java集合遍历性能

数据在内存中主要有两种存储方式: 1.顺序存储,Random Access(Direct Access) 这种方式,相邻的数据元素存放于相邻的内存地址中,整块内存地址是连续的,可以根据元素的位置直接计算出内存地址,直接进行读取.读取一个特定位置元素的平均时间复杂度为O(1).正常来说,只有基于数组实现的集合,才有这种特性.Java中以ArrayList为代表. 2.链式存储,Sequential Access: 这种方式,每一个数据元素,在内存中都不要求处于相邻的位置,每个数据元素包含他的下一个

java集合遍历删除指定元素异常分析总结

在使用集合的过程中,我们经常会有遍历集合元素,删除指定的元素的需求,而对于这种需求我们往往使用会犯些小错误,导致程序抛异常或者与预期结果不对,本人很早之前就遇到过这个坑,当时没注意总结,结果前段时间又遇到了这个问题,因此,总结下遍历集合的同时如何删除集合中指定的元素: 1.错误场景复原 public class ListRemoveTest { public static void main(String[] args) { List<User> users = new ArrayList&l

java集合遍历的几种方式总结及比较

集合类的通用遍历方式, 用迭代器迭代: Iterator it = list.iterator(); while(it.hasNext()) { Object obj = it.next(); } Map遍历方式: 1.通过获取所有的key按照key来遍历 //Set<Integer> set = map.keySet(); //得到所有key的集合 for (Integer in : map.keySet()) { String str = map.get(in);//得到每个key多对用v

Java集合遍历引发的&quot;血案&quot;

一.List集合迭代方式遍历一 <1>.可能出现的问题一:出现并发修改异常(ConcurrentModificationException) import java.util.ArrayList;  import java.util.Iterator;  import java.util.List; public class Test  {      private static List<String> list = new ArrayList<String>(); 

Java 集合

在Java Collections Framework中,不同类型的集合使用不同类型的数据结构以不同的方式存储它们的元素. 集合框架提供了遍历集合的以下方法: 使用迭代器 使用for-each循环 使用forEach()方法 使用迭代器 迭代器可以对集合执行以下三个操作: 检查是否有尚未访问的元素. hasNext() 检查是否有下一个访问的元素. next() 删除集合的最后访问元素. remove() 例子1 使用迭代器打印列表的所有元素: import java.util.ArrayLis

Java集合01----ArrayList的遍历方式及应用

                                             Java集合01----ArrayList的遍历方式及应用 1.ArrayList的遍历方式 a.一般for循环(随机访问) Integer value = null; int size = list.size(); for (int i=0; i<size; i++) { value = (Integer)list.get(i); } b.增强型for循环(for-each) Integer value

Java集合Set、List、Map的遍历方法

本文实例讲述了Java集合Set.List.Map的遍历方法,分享给大家供大家参考. 具体方法如下: 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 7