import java.util.ArrayList;
import java.util.LinkedList;
public class CollectionLinkedList {
public static void main(String[] args) {
ArrayList <Integer> aryList = new ArrayList<>();
System.out.println("亂數取出 6 個號碼 (範圍 1 到 46)");
for (int i =1;i <= 6; i++){
while(true){
int num = (int) (Math.random() * 46) + 1;
if (aryList.add(num)){
System.out.println("第 "+ i + " 個號碼" + num);
break;
}
}
}
LinkedList <Integer> queue = new LinkedList <>(aryList);
System.out.println("取出佇列 (以先進先出):");
for(int j = queue.size() - 1; j >= 0; j--){
System.out.print(queue.getFirst() + " ");
queue.removeFirst();
}
System.out.println();
LinkedList <Integer> stack = new LinkedList <>(aryList);
System.out.println("取出堆疊 (以後進先出):");
while (true){
System.out.print(stack.removeLast() + " ");
if(stack.isEmpty())
break;
}
System.out.println();
}
}
執行結果
2014年4月17日 星期四
集合 Collection:List / LinkedList
訂閱:
張貼留言 (Atom)

沒有留言:
張貼留言