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


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


描述

這個subList(int fromIndex,int toIndex)方法用於返回此 List 中 fromIndex(包含)和 toIndex(不包含)之間的部分的視圖。返回的 List 受此 List 支持,因此返回的 List 中的更改會反映在此 List 中,反之亦然。

聲明

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

public List subList(int fromIndex,int toIndex)

參數

  • fromIndex- 這是子列表的低端點(包括)。

  • toIndex- 這是子列表的高端(獨占)。

返回值

方法調用返回此列表中指定範圍的視圖。

異常

  • IndexOutOfBoundsException- 如果端點索引值超出範圍,則拋出此錯誤

  • IllegalArgumentException- 如果端點索引無序,則拋出此錯誤

示例

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

package com.tutorialspoint;

import java.util.*;

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>(8);
      List sublist = new ArrayList(10);

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

      // lets make a sublist
      sublist = vec.subList(2,6); 
      
      // let us print the size of the vector
      System.out.println("Let us print the list:"+sublist);  
   } 
}

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

Let us print the list:[2, 1, 6, 7]

相關用法


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