- Java.util 包类
- Java.util - 首页
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包其他内容
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
Java Vector clear() 方法
描述
Java Vector clear() 方法用于移除此 Vector 中的所有元素。调用此方法后,Vector 将为空,除非抛出异常。
声明
以下是 java.util.Vector.clear() 方法的声明
public void clear()
参数
此方法不接受任何参数作为输入。
返回值
返回类型为 void,因此它不返回任何值。
异常
UnsupportedOperationException − 如果 Collection 不支持 clear 方法,则会抛出此异常。
清除整数 Vector 的示例
以下示例演示了 Java Vector clear() 方法的使用。在此示例中,我们使用的是 Integer。第一步,我们填充 Vector 对象并打印它。然后我们打印 Vector 对象的大小并执行清除操作。清除后,我们打印 Vector 对象的大小,现在为 0。
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty array list Vector<Integer> vector = new Vector<>(); // use add() method to add elements in the vector vector.add(20); vector.add(30); vector.add(10); // let us print all the elements available in vector System.out.println("Vector = " + vector); // finding size of this vector int retval = vector.size(); System.out.println("Vector consists of "+ retval +" elements"); System.out.println("Performing clear operation !!"); vector.clear(); retval = vector.size(); System.out.println("Now, Vector consists of "+ retval +" elements"); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
Vector = [20, 30, 10] Vector consists of 3 elements Performing clear operation !! Now, Vector consists of 0 elements
清除字符串 Vector 的示例
以下示例演示了 Java Vector clear() 方法的使用。在此示例中,我们使用的是字符串。第一步,我们填充 Vector 对象并打印它。然后我们打印 Vector 对象的大小并执行清除操作。清除后,我们打印 Vector 对象的大小,现在为 0。
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty array list Vector<String> vector = new Vector<>(); // use add() method to add elements in the vector vector.add("A"); vector.add("B"); vector.add("C"); // let us print all the elements available in vector System.out.println("Vector = " + vector); // finding size of this vector int retval = vector.size(); System.out.println("Vector consists of "+ retval +" elements"); System.out.println("Performing clear operation !!"); vector.clear(); retval = vector.size(); System.out.println("Now, Vector consists of "+ retval +" elements"); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
Vector = [A, B, C] Vector consists of 3 elements Performing clear operation !! Now, Vector consists of 0 elements
清除对象 Vector 的示例
以下示例演示了 Java Vector clear() 方法的使用。在此示例中,我们使用的是 Student 对象。第一步,我们填充 Vector 对象并打印它。然后我们打印 Vector 对象的大小并执行清除操作。清除后,我们打印 Vector 对象的大小,现在为 0。
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String[] args) { // create an empty vector Vector<Student> vector = new Vector<>(); // use add() method to add elements in the vector vector.add(new Student(1, "Julie")); vector.add(new Student(2, "Robert")); vector.add(new Student(3, "Adam")); // let us print all the elements available in vector System.out.println("Vector = " + vector); // finding size of this vector int retval = vector.size(); System.out.println("vector consists of "+ retval +" elements"); System.out.println("Performing clear operation !!"); vector.clear(); retval = vector.size(); System.out.println("Now, vector 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 + " ]"; } }
输出
让我们编译并运行上述程序,这将产生以下结果:
Vector = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]] vector consists of 3 elements Performing clear operation !! Now, vector consists of 0 elements
java_util_vector.htm
广告