Пятница, 2025-02-07, 23:45:22
Главная Регистрация RSS
Приветствую Вас, Гость
[ Новые сообщения · Участники · Правила форума · Поиск · RSS ]
  • Страница 1 из 1
  • 1
28/11 Коллекции
CHerryДата: Понедельник, 2011-11-28, 22:07:31 | Сообщение # 1
Генералиссимус
Группа: Администраторы
Сообщений: 141
Репутация: 3732
Статус: Offline
Code
import java.util.*;

public class JCQueue
{
  public static void printQue(Queue que)
  {
   while(que.peek() != null)
    System.out.println(que.remove() + " ");
   System.out.println();
  }
   
  public static void main(String[] args) throws InterruptedException
  {
   // use timer
   System.out.println ("Queue:");
   int time=0;
   try {
    time = Integer.parseInt(args[0]);
   } catch (ArrayIndexOutOfBoundsException e) {
    // TODO Auto-generated catch block
    System.out.println("Not input parameters");
   }

     
    //int time = 10;
   Queue<Integer> queue = new LinkedList<Integer>();
   for (int i = time; i >= 0; i--)
    queue.add(i);
   while (!queue.isEmpty()) {
    System.out.println(queue.remove());
     Thread.sleep(1000);
   }

   
   System.out.println ("Deque:");
   Deque<String> deque = new ArrayDeque<String>();
   deque.add (new String ("5"));
   deque.addFirst ("A");
   deque.addLast (new String ("5"));//ошибка компиляции
   System.out.println (deque.peek ());
   System.out.println ("Before:");
   printQue(deque);
   deque.pollFirst ();
   System.out.println (deque.remove (5));
   System.out.println ("After:");
   printQue(deque);

    
   System.out.println ("PriorityQueue:");
   PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
   Random rand = new Random(47);
   for(int i=0; i<10; i++)
    pq.offer(rand.nextInt(i + 10));
   printQue(pq);

   String fact = "EDUCATION SHOULD ESCHEW OBFUSCATION";
   List<String> strings = Arrays.asList(fact.split(""));
   PriorityQueue<String> spq = new PriorityQueue<String>(strings);
   printQue(spq);
   spq = new PriorityQueue<String>(strings.size(), Collections.reverseOrder());
   spq.addAll(strings);
   printQue(spq);

   Set<Character> charSet = new HashSet<Character>();
   for(char c : fact.toCharArray())
    charSet.add(c);
   PriorityQueue<Character> cpq = new PriorityQueue<Character>(charSet);
   printQue(cpq);

    
   System.out.println ("ArrayDeque:");
   //ArrayDeque adq = new ArrayDeque<String>();
   ArrayDeque<String> adq = new ArrayDeque<String>();
   adq.push("A");
   adq.push("B");
   adq.push("C");
   adq.push("D");
   adq.push("E");
   adq.push("F");

   while(adq.peek() != null)
    System.out.println(adq.pop() + " ");
   System.out.println();
  }
}
 
  • Страница 1 из 1
  • 1
Поиск: