1.List(有序,允许重复)的遍历方式
// List的实现类
LinkedList<String> list1 = new LinkedList<String>(); // 基于双向链表的集合
ArrayList<String> list2 = new ArrayList<String>(); // 单列集合
方式一:for循环
方式二:for...each循环
方式三:迭代器遍历
2.Set(无序,不允许重复(实现类中treeSet可自动排序,LinkedHashSet有序))的遍历方式
// Set的实现类(Set接口不保证有序,SortedSet接口保证元素有序)
HashSet<String> set1 = new HashSet<String>(); // 无序,不允许重复
LinkedHashSet<String> set2 = new LinkedHashSet<String>(); // 有序,不允许重复
TreeSet<String> set3 = new TreeSet<String>(); // 有序,不允许重复(实现SortedSet接口)
方式一:for...each循环
方式二:iterator迭代器遍历
3.Map(无序,唯一(实现类中LinkedHashMap有序))的遍历方式
// Map(键值对集合)(无序且唯一)的实现类
HashMap<String, Integer> map = new HashMap<String, Integer>();
HashMap<String, String> linkedMap = new LinkedHashMap<String, String>(); // 有序
Map<String,String> treeMap = new TreeMap<String,String>(); // treeMap是Map的子类
方式一:for...each循环
方式二:遍历Map对象的entrySet()集合
4.Queue(先进先出)的遍历方式
Queue<String> queue = new LinkedList<String>();
queue.offer("yellow");
queue.offer("white");
queue.offer("pink");
queue.offer("blue");
方式1:for...each循环
方式2:iterator迭代器遍历
5.Deque(双端队列)的遍历方式
Deque<String> d = new LinkedList<String>();
d.offer("blue");
d.offer("yellow");
d.offer("white");
d.offer("pink");
方式1.for...each循环
方式2.while循环
方式3.iterator迭代器遍历
6.Stack(先进后出)的遍历方式
Stack<String> stack = new Stack<String>();
stack.push("A1");
stack.push("A2");
stack.push("A3");
stack.push("A4");
stack.push("A5");
方式1:遍历并出栈---->while循环(后进先出)
方式2:for...each循环(先进先出)
方式3:Iterator迭代器遍历(先进先出)