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


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


描述

這個remove(int index)方法用於刪除元素specified position在此 Vector 中。它將任何後續元素向左移動(從它們的索引中減去一個)。它還返回從 Vector 中刪除的元素。

聲明

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

public E remove(int index)

參數

index─ 這是要刪除的元素的索引。

返回值

方法調用返回被移除的元素。

異常

ArrayIndexOutOfBoundsException- 如果索引超出範圍,則拋出此異常。

示例

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

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);

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

      // let us remove the 1st element
      System.out.println("Removed element:"+vec.remove(1));
   }     
}

讓我們編譯並運行上麵的程序,這將產生以下結果。

Removed element:3

相關用法


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