List、Set、Map、Queue、Deque、Stack的遍历方法
一、遍历List
1,使用get(int)方法遍历,由于List的行为和数组几乎完全相同:List内部存放元素是按照元素的先后顺序进行顺序存放的,每个元素都可以通过索引确定自己的位置,List的索引和数组一样,从0开始
-
public class Main {
-
public static void main(String[] args) {
-
List<String> list = Arrays.asList("杭州", "北京", "上海", "南京");
-
for (int i=0; i<list.size(); i ) {
-
String s = list.get(i);
-
System.out.println(s);
-
}
-
}
-
}
2、使用迭代器iterator()遍历,Iterator本身是一个对象,但它是由List的实例调用iterator()方法的时候创建的。Iterator对象知道如何遍历一个List,并且不同的List类型,返回的Iterator对象也是不同的,但总是有最高的访问效率。
boolean hasnext() 判断是否由下一个元素 E next()返回下一个元素
-
public class Main {
-
public static void main(String[] args) {
-
List<String> list = Arrays.asList("杭州", "北京", "上海", "南京");
-
for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
-
String s = it.next();
-
System.out.println(s);
-
}
-
}
-
}
二、遍历Set
Set是Collection下面的无序子接口
特点:无序、没有索引、不能重复
1、使用toArray(),转换成一个数组,对数组进行遍历;
-
public class Main {
-
public static void main(String[] args) {
-
Set<String> set = new HashSet<String>();
-
Object[ ] objects = set.toArray();
-
for(int i = 0; i < objects.length; i ){
-
System.out.println(objects[i] "");
-
}
-
}
-
}
2、使用迭代器iterator()遍历
-
public class Main {
-
public static void main(String[] args) {
-
Set<String> set = new HashSet<String>();
-
for (Iterator<String> it = set.iterator(); it.hasNext(); ) {
-
String s = it.next();
-
System.out.println(s);
-
}
-
}
-
}
3、使用foeach循环进行遍历
-
public class Main {
-
public static void main(String[] args) {
-
Set<String> set = new HashSet<String>();
-
for (String s : set){
-
System.out.println(s);
-
}
-
}
-
}
三、遍历Map
1、使用foreach循环遍历Map实例的keySet()方法返回的Set集合
-
public class Main {
-
public static void main(String[] args) {
-
Map<String,Integer> map = new HashMap<>();
-
for (String key : map.keySet()){
-
Integer value = map.get(key);
-
System.out.println(key "=" value);
-
}
-
}
-
}
2、遍历使用foreach循环遍历Map对象的entrySet()集合,它包含每一个key-value的映射
-
public class Main {
-
public static void main(String[] args) {
-
Map<String,Integer> map= new HashMap<>();
-
for(Map.Entry<String,Integer> entry : map.entrySet())){
-
String key = entry.getKey();
-
Integer value = entry.getValue();
-
System.out.println(key "=" value);
-
}
-
}
-
}
四、遍历Queue
1、使用foreach循环遍历queue
-
public class Main {
-
public static void main(String[] args) {
-
Queue<String> queue = new LinkedList<String>();
-
for(String temp:queue){
-
System.out.println(temp);
-
}
-
}
-
}
2、使用迭代器iterator()遍历
-
-
public class Main {
-
public static void main(String[] args) {
-
Queue<String> queue = new LinkedList<String>();
-
Iterator it = queue.iterator();
-
while(it.hasnext()){
-
System.out.println(it.next);
-
}
-
}
-
}
3、使用队列Queue的自带方法
boolean isEmpty() 判断队列中元素是否为空
E poll() 取出队首元素并删除
-
-
public class Main {
-
public static void main(String[] args) {
-
Queue<String> queue = new LinkedList<String>();
-
while(!queue.isEmpty()){
-
System.out.println(queue.poll());
-
}
-
}
-
}
五、遍历Deque
1、foreach循环遍历
-
public class Main {
-
public static void main(String[] args) {
-
Deque<String> deque = new LinkedList<String>();
-
for (String string : deque) {
-
System.out.println(string);
-
}
-
}
-
}
2、使用迭代器遍历
-
public class Main {
-
public static void main(String[] args) {
-
Deque<String> deque = new LinkedList<String>();
-
Iterator<String> it = deque.iterator();
-
while(it.hasNext()) {
-
System.out.println(it.next());
-
}
-
}
-
}
六、遍历stack
1、while循环遍历
-
-
public class Main {
-
public static void main(String[] args) {
-
Stack<String> stack = new Stack<String>();
-
while(stack.isEmpty()) {
-
System.out.println(stack.pop());
-
}
-
}
-
}
-
-
2、foreach循环遍历
-
-
public class Main {
-
public static void main(String[] args) {
-
Stack<String> stack = new Stack<String>();
-
Iterator<String> it = stack.iterator();
-
while(it.hasNext()) {
-
System.out.println(it.next());
-
}
-
}
-
}
-
-
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgcfecj
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01