📜  Java中的 ArrayList subList() 方法及示例

📅  最后修改于: 2022-05-13 01:55:04.576000             🧑  作者: Mango

Java中的 ArrayList subList() 方法及示例

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

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

句法:

public List subList(int fromIndex, int toIndex)

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

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

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

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

  • 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
            ArrayList
                arrlist = new ArrayList();
  
            // 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 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
            ArrayList
                arrlist = new ArrayList();
  
            // 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 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
            ArrayList
                arrlist = new ArrayList();
  
            // 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 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)