1. 程式人生 > 實用技巧 >Java 面試常見題【未記錄完】

Java 面試常見題【未記錄完】

Queue常用方法:

  • queues.add("1");// 尾插--集合中新增元素
  • String str = queues.peek();//返回頭--返回此佇列的頭
  • queues.poll();//頭刪--檢索並刪除此佇列的頭
  • queues.offer("4");// 尾插--將指定的元素插入此佇列尾部
  • queues.remove();//頭刪--檢索並刪除此佇列的頭
  • queues.remove("4");//刪除指定--從中刪除指定元素的單個例項
  • boolean isNull = queues.isEmpty();//判空--集合是否為空
  • queues.clear();//清除集合--刪除此集合中的所有元素,此方法返回後,集合將為空。
 public static void queueTest() {
        // 佇列繼承Collection
        Queue<String> queues = new LinkedList<>();
        queues.add("1");// 集合中新增元素
        queues.add("2");
        queues.add("3");
        String str = queues.peek();// 返回此佇列的頭
        System.out.println("此佇列的頭str:" + str);
        queues.poll();
// 檢索並刪除此佇列的頭>>Retrieves and removes the head of this queue queues.offer("4");// 如果可能,請將指定的元素插入此佇列 queues.remove();// 檢索並刪除此佇列的頭 queues.remove("4");// 從中刪除指定元素的單個例項 boolean isNull = queues.isEmpty();// 集合是否為空 System.out.println("isNull" + isNull); queues.clear();
// 刪除此集合中的所有元素,此方法返回後,集合將為空。 isNull = queues.isEmpty();// 集合是否為空 System.out.println("isNull" + isNull); }