当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ArrayList subList()用法及代码示例


java.util.ArrayList类的subList()方法用于返回此列表中指定的fromIndex(包括)和toIndex(不包括)之间的视图。 (如果fromIndex和toIndex相等,则返回的列表为空。)

返回列表由该列表支持,因此返回列表中的非结构更改会反映在此列表中,反之亦然。返回的列表支持所有可选列表操作。

用法:


public List subList(int fromIndex, int toIndex)

参数:此方法将以下参数作为参数。

  • fromIndex –subList的低端点(包括端点)
  • toIndex –subList的高端端点(不包括)

返回值:此方法返回此列表内指定范围的视图。

异常:此方法引发以下Exception。

  • 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 ArrayList<Integer> 
            ArrayList<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 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); 
        } 
  
        catch (IllegalArgumentException 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.*; 
  
public class GFG1 { 
    public static void main(String[] argv) 
        throws Exception 
    { 
        try { 
  
            // Creating object of ArrayList<Integer> 
            ArrayList<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 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); 
        } 
  
        catch (IllegalArgumentException 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: 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 ArrayList<Integer> 
            ArrayList<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 arrlist: "
                               + 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 arrlist: "
                               + arrlist2); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
            System.out.println("Exception thrown: " + e); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("Exception thrown: " + e); 
        } 
    } 
}
输出:
Original arrlist: [A, B, C, D, E]

Endpoint indices are out of order (fromIndex > toIndex)
Exception thrown: java.lang.IllegalArgumentException: fromIndex(7) > toIndex(2)


相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 ArrayList subList() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。