📌  相关文章
📜  Java中的 CopyOnWriteArrayList subList() 方法及示例

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

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

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

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

句法:

public List subList(int fromIndex, int toIndex)

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

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

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

异常:如果端点索引值超出范围(fromIndex 大小),此方法将引发IndexOutOfBoundsException

下面是说明subList()方法的示例。

示例 1:

// Java program to demonstrate
// subList() method
// for String value
  
import java.util.*;
import java.util.concurrent.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of CopyOnWriteArrayList
            CopyOnWriteArrayList
                arrlist = new CopyOnWriteArrayList();
  
            // 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);
        }
    }
}
输出:
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.*;
import java.util.concurrent.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
  
            // Creating object of CopyOnWriteArrayList
            CopyOnWriteArrayList
                arrlist = new CopyOnWriteArrayList();
  
            // 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);
        }
    }
}
输出:
Original arrlist: [A, B, C, D, E]

End index value is out of range
Exception thrown: java.lang.IndexOutOfBoundsException