Java ArrayDeque clear() 方法



描述

Java ArrayDeque clear() 方法会移除此双端队列中的所有元素。当我们想要重用队列或重置队列时,此方法非常有用。

声明

以下是java.util.ArrayDeque.clear() 方法的声明

public void clear()

参数

返回值

此方法不返回任何值。

异常

清除整数型 ArrayDeque 示例

以下示例演示了 Java ArrayDeque clear() 方法的使用。在这个示例中,我们使用的是 Integer。首先,我们填充 arraydeque 对象并打印它。然后我们打印 arraydeque 对象的大小并执行清除操作。清除后,我们打印 arraydeque 对象的大小,现在为 0。

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeDemo {
   public static void main(String[] args) {
     
      // create an empty array deque
      Deque<Integer> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(20);
      deque.add(30);
      deque.add(10);

      // let us print all the elements available in deque
      System.out.println("ArrayDeque = " + deque);    

      // finding size of this deque
      int retval = deque.size();
      System.out.println("Deque consists of "+ retval +" elements");

      System.out.println("Performing clear operation !!");
      deque.clear();
      retval = deque.size();
      System.out.println("Now, deque consists of "+ retval +" elements");
   }
}   

输出

让我们编译并运行上述程序,这将产生以下结果:

ArrayDeque = [20, 30, 10]
Deque consists of 3 elements
Performing clear operation !!
Now, deque consists of 0 elements

清除字符串型 ArrayDeque 示例

以下示例演示了 Java ArrayDeque clear() 方法的使用。在这个示例中,我们使用的是 String。首先,我们填充 arraydeque 对象并打印它。然后我们打印 arraydeque 对象的大小并执行清除操作。清除后,我们打印 arraydeque 对象的大小,现在为 0。

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeDemo {
   public static void main(String[] args) {
     
      // create an empty array deque
      Deque<String> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add("A");
      deque.add("B");
      deque.add("C");

      // let us print all the elements available in deque
      System.out.println("ArrayDeque = " + deque);    

      // finding size of this deque
      int retval = deque.size();
      System.out.println("Deque consists of "+ retval +" elements");

      System.out.println("Performing clear operation !!");
      deque.clear();
      retval = deque.size();
      System.out.println("Now, deque consists of "+ retval +" elements");
   }
}   

输出

让我们编译并运行上述程序,这将产生以下结果:

ArrayDeque = [A, B, C]
Deque consists of 3 elements
Performing clear operation !!
Now, deque consists of 0 elements

清除对象型 ArrayDeque 示例

以下示例演示了 Java ArrayDeque clear() 方法的使用。在这个示例中,我们使用的是 Student 对象。首先,我们填充 arraydeque 对象并打印它。然后我们打印 arraydeque 对象的大小并执行清除操作。清除后,我们打印 arraydeque 对象的大小,现在为 0。

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeDemo {
   public static void main(String[] args) {
     
      // create an empty array deque
      Deque<Student> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(new Student(1, "Julie"));
      deque.add(new Student(2, "Robert"));
      deque.add(new Student(3, "Adam"));

      // let us print all the elements available in deque
      System.out.println("ArrayDeque = " + deque);    

      // finding size of this deque
      int retval = deque.size();
      System.out.println("Deque consists of "+ retval +" elements");

      System.out.println("Performing clear operation !!");
      deque.clear();
      retval = deque.size();
      System.out.println("Now, deque consists of "+ retval +" elements");
   }
} 

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}  

输出

让我们编译并运行上述程序,这将产生以下结果:

ArrayDeque = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Deque consists of 3 elements
Performing clear operation !!
Now, deque consists of 0 elements
java_util_arraydeque.htm
广告