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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。