當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.Vector.isEmpty()用法及代碼示例


描述

這個isEmpty()方法用於測試此向量是否沒有分量。

聲明

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

public boolean isEmpty()

參數

NA

返回值

當且僅當此向量沒有分量,即其大小為零時,返回值為真。否則返回假。

異常

NA

示例

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

package com.tutorialspoint;

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<Integer> vec = new Vector<Integer>(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

相關用法


注:本文由純淨天空篩選整理自 java.util.Vector.isEmpty() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。