java.util.CopyOnWriteArrayList類的subList()方法用於返回此列表中指定的fromIndex(包括)和toIndex(不包括)之間的視圖。 (如果fromIndex和toIndex相等,則返回的列表為空。)
返回列表由該列表支持,因此返回列表中的非結構更改會反映在此列表中,反之亦然。返回的列表支持所有可選列表操作。
用法:
public List subList(int fromIndex, int toIndex)
參數:此方法將以下參數作為參數。
- fromIndex –subList的低端點(包括端點)
- toIndex –subList的高端端點(不包括)
返回值:此方法返回此列表內指定範圍的視圖。
異常:如果端點索引值超出範圍(fromIndex大小),則此方法引發IndexOutOfBoundsException
以下示例說明了subList()方法。
示例1:
// Java program to demonstrate
// subList() method
// for String value
import java.util.*;
import java.util.concurrent.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Creating object of CopyOnWriteArrayList<Integer>
CopyOnWriteArrayList<String>
arrlist = new CopyOnWriteArrayList<String>();
// Populating arrlist1
arrlist.add("A");
arrlist.add("B");
arrlist.add("C");
arrlist.add("D");
arrlist.add("E");
// print arrlist
System.out.println("Original arrlist: "
+ arrlist);
// getting the subList
// using subList() method
List<String> arrlist2 = arrlist.subList(2, 4);
// print the subList
System.out.println("Sublist of arrlist: "
+ arrlist2);
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception thrown: " + e);
}
}
}
輸出:
Original arrlist: [A, B, C, D, E] Sublist of arrlist: [C, D]
示例2:對於IndexOutOfBoundsException
// Java program to demonstrate
// subList() method
// for IndexOutOfBoundsException
import java.util.*;
import java.util.concurrent.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Creating object of CopyOnWriteArrayList<Integer>
CopyOnWriteArrayList<String>
arrlist = new CopyOnWriteArrayList<String>();
// Populating arrlist1
arrlist.add("A");
arrlist.add("B");
arrlist.add("C");
arrlist.add("D");
arrlist.add("E");
// print arrlist
System.out.println("Original arrlist: "
+ arrlist);
// getting the subList
// using subList() method
System.out.println("\nEnd index value is out of range");
List<String> arrlist2 = arrlist.subList(2, 7);
// print the subList
System.out.println("Sublist of arrlist: "
+ arrlist2);
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception thrown: " + e);
}
}
}
輸出:
Original arrlist: [A, B, C, D, E] End index value is out of range Exception thrown: java.lang.IndexOutOfBoundsException
相關用法
- Java CopyOnWriteArrayList set()用法及代碼示例
- Java List sublist()用法及代碼示例
- Java ArrayList subList()用法及代碼示例
- Java AbstractList subList()用法及代碼示例
- Java CopyOnWriteArrayList removeAll()用法及代碼示例
- Java CopyOnWriteArrayList removeIf()用法及代碼示例
- Java CopyOnWriteArrayList equals()用法及代碼示例
- Java CopyOnWriteArrayList retainAll()用法及代碼示例
- Java CopyOnWriteArrayList addAllAbsent()用法及代碼示例
- Java CopyOnWriteArrayList remove()用法及代碼示例
- Java CopyOnWriteArrayList forEach()用法及代碼示例
- Java CopyOnWriteArrayList addAll()用法及代碼示例
- Java CopyOnWriteArrayList add()用法及代碼示例
- Java CopyOnWriteArrayList contains()用法及代碼示例
- Java CopyOnWriteArrayList get()用法及代碼示例
注:本文由純淨天空篩選整理自suman_ptnl大神的英文原創作品 CopyOnWriteArrayList subList() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。