当前位置:网站首页>List,Set,Map,Queue,Deque,Stack遍历方式总结
List,Set,Map,Queue,Deque,Stack遍历方式总结
2022-08-09 15:01:00 【鸣筝鸣筝】
List遍历方式
方式一:for循环
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}方式二:for-each循环
for(String s:list){
System.out.println(s);
}方式三:迭代器(Iterator)
Iterator<String> it=list.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}Set遍历方式
方式一:for-each循环
for(String s:list){
System.out.println(s);
}方式二:迭代器(iterator)
Iterator<String> it=set.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}Map遍历方式
HashMap<String, Integer> map=new HashMap<String, Integer>();
map.put("长津湖", 13000);
map.put("大耳朵图图", 13000);方式一:entrySet()
for (Entry<String, Integer> kv :map.entrySet()) {
System.out.println(kv.getKey());
System.out.println(Arrays.toString(kv.getValue()));
}方式二:for-each循环
//key
//key
for(String key:map.keySet()){
System.out.println("key:"+key+" "+"Value:"+map.get(key))
}
//value
for(Integer value:map.values){
System.out.println(value)
}方式三:迭代器(iterator)
Iterator<Entry<String, Integer>> entries = map.entrySet().iterator();
while(entries.hasNext()){
Entry<String, Integer> entry = entries.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+":"+value);
}Queue遍历方式
Queue(接口)遵循先进先出(FIFO)原则,单向出队,前端出队后端入队。
Queue<String> queue=new LinkedList<String>();
queue.offer("A1");
queue.offer("A2");
queue.offer("A3");方式一:for-each循环
for (String q : queue) {
System.out.println(q);
}方式二:迭代器(iterator)
Iterator it = queue.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}方式三:while循环
使用isEmpty()方法判断队列是否为空,然后出队遍历
while (!queue.isEmpty()) {
System.out.println(queue.poll());
}Deque遍历方式
Deque(接口)双端队列,与Queue不同点在于,Deque可以从俩端进行出队与入队是双向的。
Deque<String> deque=new LinkedList<String>();
deque.offerFirst("B1");//队头
deque.offerLast("B2");//队尾
deque.offer("B3");//队尾方式一:for-each循环
for(String s: deque) {
System.out.println(s);
}方式二:while循环(出队遍历)
String item=null;
while((item=deque.pollLast())!=null) {
System.out.println(item);
}方式三:迭代器(iterator)
Iterator<String> it =deque.iterator();
while(it.hasNext()) {
System.out.println(it.next());
} Stack遍历方式
Stack<String> stack=new Stack<String>();
stack.push("A1");
stack.push("A2");
stack.push("A3");方式一:for-each循环
for(String s: stack) {
System.out.println(s);
} 方式二:while循环 (遍历并出栈)
while(!stack.isEmpty()) {
System.out.println(stack.pop());
}方式三:迭代器(iterator)
Iterator<String> it=stack.iterator();
while(it.hasNext()) {
System.out.println(it.next());
} 边栏推荐
猜你喜欢

客户端媒体引擎框架

【力扣】114. 二叉树展开为链表

weiteUP-ciscn_2019_c_1

hugging face tutorial - Chinese translation - share a model

Vim实用技巧_4.管理多个文件(打开 + 切分 + 保存 + netrw)

The practical skills Vim _5. Move quickly between files and documents

【剑指 Offer】 37. 序列化二叉树

Face recognition sample code analysis (2) - face recognition analysis

Detailed Explanation of Software Secure Memory Area

堆(heap)系列_0x07:NT堆调试支持_滞后发现调试支持
随机推荐
Introduction to common commands in SQLMap
Vim实用技巧_6.复制和粘贴原理(寄存器)
交叉编译 OpenSSL
【力扣】96. 不同的二叉搜索树
在任务管理器中结束任务进程之后电脑直接黑屏了
堆(heap)系列_0x09:堆破坏示例(非法访问+未初始化+堆句柄不匹配)
Heap series _0x02: The past and present of the heap (WinDbg+Visual Studio compilation)
Cloud Models and Logistic Regression - Applications of MATLAB in Mathematical Modeling (2nd Edition)
Vim实用技巧_0.vim - introduction
go使用Consul实用指南
蒙特卡罗 Monte Carlo 模拟
GStreamer应用开发手册学习笔记之二
【QT】窗口的显示与模态窗口
Word 2016 撰写论文(1): 公式居中、编号右对齐
客户端媒体引擎框架
The experience of using Photoshop CS6
【力扣】11. 盛最多水的容器
【Postgraduate Work Weekly】(Week 5)
Unity Shader零基础入门1:纯色物体
数据库导入导出sql数据库文件