- 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 isEmpty() 方法
描述
Java Vector isEmpty() 方法用于测试此向量是否没有组件。
声明
以下是 java.util.Vector.isEmpty() 方法的声明
public boolean isEmpty()
参数
无
返回值
当且仅当此向量没有组件(即大小为零)时,返回值为 true。否则返回 false。
异常
无
检查整数向量的空性示例
以下示例演示了 Java Vector isEmpty() 方法的用法。这里我们使用的是整数向量。首先,我们初始化一个向量对象,然后检查它是否为空。然后我们将添加一些元素,然后再次检查向量对象是否为空。
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<>(); if (vector.isEmpty()) { System.out.println("vector is empty"); } else { System.out.println("vector is not empty"); } // printing all the elements available in vector System.out.println("Vector = " + vector); // use add() method to add elements in the vector vector.add(0); vector.add(1); vector.add(2); vector.add(3); vector.add(4); vector.add(5); vector.add(6); if (vector.isEmpty()) { System.out.println("vector is empty"); } else { System.out.println("vector is not empty"); } // printing all the elements available in vector System.out.println("Vector = " + vector); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
vector is empty Vector = [] vector is not empty Vector = [0, 1, 2, 3, 4, 5, 6]
检查整数向量的空性示例
以下示例演示了 Java Vector isEmpty() 方法的用法。这里我们使用的是字符串向量。首先,我们初始化一个向量对象,然后检查它是否为空。然后我们将添加一些元素,然后再次检查向量对象是否为空。
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<>(); if (vector.isEmpty()) { System.out.println("vector is empty"); } else { System.out.println("vector is not empty"); } // printing all the elements available in vector System.out.println("Vector = " + vector); // use add() method to add elements in the vector vector.add("A"); vector.add("B"); vector.add("C"); if (vector.isEmpty()) { System.out.println("vector is empty"); } else { System.out.println("vector is not empty"); } // printing all the elements available in vector System.out.println("Vector = " + vector); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
vector is empty Vector = [] vector is not empty Vector = [A, B, C]
检查整数向量的空性示例
以下示例演示了 Java Vector isEmpty() 方法的用法。这里我们使用的是 Student 对象向量。首先,我们初始化一个向量对象,然后检查它是否为空。然后我们将添加一些元素,然后再次检查向量对象是否为空。
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<>(); if (vector.isEmpty()) { System.out.println("vector is empty"); } else { System.out.println("vector is not empty"); } // printing all the elements available in vector System.out.println("Vector = " + 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")); if (vector.isEmpty()) { System.out.println("vector is empty"); } else { System.out.println("vector is not empty"); } // printing all the elements available in vector System.out.println("Vector = " + vector); } } 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 + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
vector is empty Vector = [] vector is not empty Vector = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_vector.htm
广告