java中subList用法_java.util.Vector.subList()方法实例

全屏

subList(int fromIndex,int toIndex)方法用于返回fromIndex(包括),以及则为toIndex(不包括)列表的部分视图。返回由此List支持的List,因此改变了返回的列表将反映在此列表中,反之亦然。

声明

以下是java.util.Vector.subList()方法的声明public List subList(int fromIndex,int toIndex)

参数fromIndex--这是子列表的低端点(包括)。

toIndex--这是子列表的高点(不包括)。

返回值

该方法调用返回指定范围内List的视图。

异常IndexOutOfBoundsException--若终端索引值超出范围,这是将会被抛出。

IllegalArgumentException--这被抛出,如果终结点索引是无序的。

例子

下面的例子显示java.util.Vector.subList()方法的使用。package cn.sxt;

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(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]

分享到:

0评论

14487a65ea137d8f9ac97cdce44a0324.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值