當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。