位置:首頁 > Java技術 > Java.util包 > java.util.Vector.isEmpty()方法實例

java.util.Vector.isEmpty()方法實例

isEmpty() 方法用於測試此向量是否不包含組件(是一個空向量)。

聲明

以下是java.util.Vector.isEmpty()方法的聲明

public boolean isEmpty()

參數

  • NA

返回值

返回值是true當且僅當此向量冇有組件,也就是說,它的大小是零。否則返回false。

異常

  • NA

例子

下麵的例子顯示java.util.Vector.isEmpty()方法的使用。

package com.yiibai;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      // create an empty Vector vec with an initial capacity of 4      
      Vector vec = new Vector(4);
      
      // let us test the vector
      System.out.println("The vector does not have any element"); 
      System.out.println("The vector is empty: "+vec.isEmpty()); 

      // use add() method to add elements in the vector
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);

      // let us test the vector again
      System.out.println("The vector has elements"); 
      System.out.println("The vector is empty: "+vec.isEmpty()); 
   }     
}

現在編譯和運行上麵的代碼示例,將產生以下結果。

The vector does not have any element
The vector is empty: true
The vector has elements
The vector is empty: false