日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java List部分截取,获得指定长度子集合

發(fā)布時間:2025/3/15 java 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java List部分截取,获得指定长度子集合 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

subList方法用于獲取列表中指定范圍的子列表,該列表支持原列表所支持的所有可選操作。返回列表中指定范圍的子列表。

?語法?

? ? ? ? subList(int fromIndex, int toIndex)?

? ? ? ? fromIndex:用于指定新列表的起始點(diǎn)(包括該點(diǎn))。

? ? ? ? toIndex:用于指定新列表的結(jié)束點(diǎn)(不包括該點(diǎn))。

用法實例:

? ? public static void main(String[] args) {
? ? ? ? ? ? ? ? ? ? ? ? ?List<String> list = new ArrayList<String>();
? ? ? ? ? ? ? ? ? ? ? ? ? list.add("貓");??????? //向列表中添加數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? ? ? list.add("狗");? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? list.add("豬");? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? list.add("鴨子");? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? list.add("雞");? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? list.add("猴子");? ? ??
? ? ? ? ? ? ? ? ? ? ?Iterator<String> its = list.iterator();? ? //迭代list
? ? ? ? ? ? ? ? ? ? ? ?System.out.println("集合中所有元素對象:");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? while (its.hasNext()) {??????? //循環(huán)遍歷集合
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.print(its.next() + "? ");???? //輸出集合內(nèi)容
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? List<String> subList = list.subList(3, 5);??? //獲取子列表
? ? ? ? ? ? ? ? ? System.out.println("\n截取集合中部分元素:");
? ? ? ? ? ? ? ? ? ? ? ? Iterator it = subList.iterator();
? ? ? ? ? ? ? ? ? ? ? ? ? ? while (it.hasNext()) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.print(it.next() + "? ");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? }

轉(zhuǎn)載于:https://www.cnblogs.com/TheGCC/p/8840256.html

總結(jié)

以上是生活随笔為你收集整理的Java List部分截取,获得指定长度子集合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。