- Java.util Package Classes
- Java.util - Home
- 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 Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Vector isEmpty() Method
Description
The Java Vector isEmpty() method is used to tests if this vector has no components.
Declaration
Following is the declaration for java.util.Vector.isEmpty() method
public boolean isEmpty()
Parameters
NA
Return Value
The return value is true if and only if this vector has no components, that is, its size is zero.Otherwise it returns false.
Exception
NA
Checking Emptyness of a Vector of Integer Example
The following example shows the usage of Java Vector isEmpty() method. Here we are working with an Vector of Integers. At first, we initialize an Vector object and then check if it is empty or not. Then we'll be adding few elements and then check again if Vector object is empty or not.
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); } }
Output
Let us compile and run the above program, this will produce the following result −
vector is empty Vector = [] vector is not empty Vector = [0, 1, 2, 3, 4, 5, 6]
Checking Emptyness of a Vector of Integer Example
The following example shows the usage of Java Vector isEmpty() method. Here we are working with an Vector of Strings. At first, we initialize an Vector object and then check if it is empty or not. Then we'll be adding few elements and then check again if Vector object is empty or not.
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); } }
Output
Let us compile and run the above program, this will produce the following result −
vector is empty Vector = [] vector is not empty Vector = [A, B, C]
Checking Emptyness of a Vector of Integer Example
The following example shows the usage of Java Vector isEmpty() method. Here we are working with an Vector of Student objects. At first, we initialize an Vector object and then check if it is empty or not. Then we'll be adding few elements and then check again if Vector object is empty or not.
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); } }
Output
Let us compile and run the above program, this will produce the following result −
vector is empty Vector = [] vector is not empty Vector = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
To Continue Learning Please Login
Login with Google