List Queue Map Stack的遍歷


package Test01;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListDemo01 {
public static void main(String[] args) {
List<String>list=new ArrayList<String>();
for(int i=0;i<5;i++){
list.add(i+"位");
}
//普通for
for (int i = 0; i < list.size();i++) {
System.out.print(list.get(i)+" ");
}
System.out.println();
for (String str : list) {
System.out.print(str+" ");
}
System.out.println();
//iterator
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();

for(list.iterator();iterator.hasNext();){
System.out.print(iterator.next()+" ");
}
System.out.println();

}

}

package Test01;

import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;

public class QueueDemo01 {
public static void main(String[] args) {
Queue<String>queue=new LinkedBlockingDeque<String>();
for(int i=0;i<5;i++){
queue.offer(i+"位");
}
//集合遍歷
for (String str : queue) {
System.out.print(str+" ");
}
System.out.println();

Iterator<String> iterator = queue.iterator();
while(iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();

//隊列方式遍歷
while(queue.peek()!=null){
System.out.print(queue.poll()+" ");
}
System.out.println();

}
}

package Test01;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapDemo01 {
public static void main(String[] args) {
Map<Integer, String>map=new HashMap<Integer, String>();
map.put(1, "name");
map.put(2, "age");
map.put(3, "sex");
map.put(4, "phone");
//entrySet()
Set<Entry<Integer,String>> entrySet = map.entrySet();
for (Entry<Integer, String> entry : entrySet) {
System.out.println(entry.getKey()+" "+entry.getValue());
}
System.out.println();
//entrySet()和iterator
Iterator<Entry<Integer, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Entry<Integer, String> next = iterator.next();
System.out.println(next.getKey()+" "+next.getValue());
}
System.out.println();

//keySet()
Set<Integer> keySet = map.keySet();
for (Integer key : keySet) {
System.out.println(key+" "+map.get(key));
}
System.out.println();

//keySet()和iterator
Iterator<Integer> iterator2 = map.keySet().iterator();
for (;iterator2.hasNext();) {
Integer next = iterator2.next();
System.out.println(next+" "+map.get(next));
}
}

}

package Test01;

import java.util.Iterator;
import java.util.Stack;

public class StackDemo01 {
public static void main(String[] args) {
Stack<Integer>stack=new Stack<Integer>();
for(int i=0;i<10;i++){
stack.push(i);
}

//集合遍歷方式
for (Integer integer : stack) {
System.out.print(integer+" ");
}
System.out.println();

//使用迭代器
Iterator<Integer> iterator = stack.iterator();
while(iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
System.out.println();

Iterator<Integer> iterator2 = stack.iterator();
for(;iterator2.hasNext();){
System.out.print(iterator2.next()+" ");
}
System.out.println();

//棧彈出方式
while(!stack.empty()){
System.out.print(stack.pop()+" ");
}

//while(stack.peek()!=null)不要這樣寫
}
}

0 個評論

要回覆文章請先登錄註冊