codecamp

Groovy subList()方法

返回此指定的fromIndex(包括)和toIndex(排除)之间的此范围部分的视图。

句法

List subList(int fromIndex, int toIndex)

参数

  • fromIndex - 范围的起始索引
  • toIndex - End范围的索引

返回值

从指定的开始到结束索引的范围值的列表。

下面是一个使用这个方法的例子 -

class Example { 
   static void main(String[] args) { 
      def rint = 1..10; 
		
      println(rint.subList(1,4)); 
      println(rint.subList(4,8)); 
   } 
}

当我们运行上面的程序,我们将得到以下结果 -

[2, 3, 4] 
[5, 6, 7, 8] 
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定