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