CodeGym /Java 博客 /随机的 /Java 中的 Sublist() 方法:ArrayList 和 List
John Squirrels
第 41 级
San Francisco

Java 中的 Sublist() 方法:ArrayList 和 List

已在 随机的 群组中发布

什么是 subList() 方法?

集合框架是 Java API 中非常流行的组件。List 接口和 ArrayList 类可能是 Collections Framework 中最重要的工具。subList是 List 接口中的一种方法,可让您从现有列表的一部分创建新列表。但是,这个新创建的列表只是一个引用原始列表的视图。Java 中的 Sublist() 方法:ArrayList 和 List - 1例如,以 [1,2,3,4,5,6] 的列表为例。假设您要创建一个没有第一个和最后一个元素的新列表。在这种情况下,list.subList()方法会帮助你。subList(fromIndex, toIndex)方法只有一种形式,它有两个参数,分别是第一个索引(fromIndex)和最后一个索引(到索引)它会将fromIndextoIndex之间的部分作为新列表返回。有一点要记住。新创建的列表将包含 fromIndex 并排除 toIndex。所以上述场景的算法将是这样的。List = [1,2,3,4,5,6] newList = List.subList(1,5) 由于subList是List 接口的方法,因此可以在ArrayList、LinkedList、Stack 和Vector 对象上使用它。但是,在本文中,我们将主要关注 ArrayList 和 LinkedList 对象。

ArrayList 对象的 subList 方法示例。

我们正在声明一个国家 ArrayList。然后我们尝试返回第 2 个和第 4 个元素之间的部分。

import java.util.*;
 
public class Main {
    public static void main(String[] args) {
         // create an ArrayList 
        ArrayList list = new ArrayList();
         // add values to ArrayList
         list.add("USA");
         list.add("UK");
         list.add("France");
         list.add("Germany");
         list.add("Russia");
        System.out.println("List of the countries:" + list);
         //Return the subList : 1 inclusive and 3 exclusive
        ArrayList new_list = new  ArrayList(list.subList(1, 3));
        System.out.println("The subList of the list: "+new_list);
    }
 }
上面代码的输出将是
国家列表:[美国、英国、法国、德国、俄罗斯] 列表子列表:[英国、法国]
在ArrayList中,第一个元素的索引值为0。因此,第二个和第四个元素的索引值分别为1和3。因此,我们调用sublist()方法作为list.subList(1, 3)。但是,请记住 subList 方法返回不包括 toIndex 的部分,在本例中是第四个元素(“Germany”) 。因此它只会输出“UK”“France”。由于返回的输出是一个 List 本身,您可以直接在其上调用任何 List 方法。那么如果我们对两个参数使用相同的索引会发生什么?该索引将包含或排除在返回列表中吗?让我们找出来。

//execute subList() method with the same argument for both parameters.
ArrayList new_list2 = new ArrayList(list.subList(3, 3));
System.out.println("The subList of the list: "+new_list2);
输出是
列表的子列表:[ ]
输出是一个空列表。即使 fromIndex 选择了第 4 个元素,subList()方法也会将其删除,因为它也是 toIndex。

LinkedList 对象上的 subList 方法示例。

在此示例中,我们将在 LinkedList 元素上使用 sublist 方法。同样,它将返回指定索引fromIndex(inclusive)toIndex(exclusive)之间的列表。请记住,我们说过subList()方法返回的列表只是一个具有对原始列表的引用的视图。如果您对子列表进行任何更改,它也会影响原始列表。我们也将在这个例子中测试它。

import java.util.LinkedList;
import java.util.Iterator;
import java.util.List;
 
public class Main {
 
 public static void main(String[] args) {
 
    // Create a LinkedList
    LinkedList linkedlist = new LinkedList();
 
    // Add elements to LinkedList
    for(int i = 0; i<7; i++){
      linkedlist.add("Node "+ (i+1));
    }
 
    // Displaying LinkedList elements
    System.out.println("Elements of the LinkedList:");
    Iterator it= linkedlist.iterator();
    while(it.hasNext()){
       System.out.print(it.next()+ " ");
    }
 
    // invoke subList() method on the linkedList
    List sublist = linkedlist.subList(2,5);
 
    // Displaying SubList elements
    System.out.println("\nElements of the sublist:");
    Iterator subit= sublist.iterator();
    while(subit.hasNext()){
       System.out.print(subit.next()+" ");
    }
 
    /* The changes you made to the sublist will affect the     original LinkedList
     * Let’s take this example - We
     * will remove the element "Node 4" from the sublist.
     * Then we will print the original LinkedList. 
     * Node 4 will not be in the original LinkedList too. 
     */
    sublist.remove("Node 4");
    System.out.println("\nElements of the LinkedList LinkedList After removing Node 4:");
    Iterator it2= linkedlist.iterator();
    while(it2.hasNext()){
       System.out.print(it2.next()+" ");
    }
 }
}
输出将如下所示:
链表的元素: 节点1 节点2 节点3 节点4 节点5 节点6 节点7 子列表的元素: 节点3 节点4 节点5 链表的元素删除节点4 后的链表: 节点1 节点2 节点3 节点5 节点6节点 7

如果 subList() 中的索引超出范围会发生什么?

subList方法返回两种类型的异常让我们来看看它们。考虑指定索引超出 List 元素范围(fromIndex < 0 || toIndex > size)的情况。然后它会抛出一个IndexOutOfBoundExecption

//using subList() method with fromIndex <0 
ArrayList new_list2 = new ArrayList(list.subList(-1, 3));
System.out.println("Portion of the list: "+new_list2);
 
Exception in thread "main" java.lang.IndexOutOfBoundsException: fromIndex = -1
 
// using subList() method with toIndex > size
ArrayList new_list2 = new ArrayList(list.subList(3, 6));
System.out.println("Portion of the list: "+new_list2);
 
Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 6
此外,如果 fromIndex 大于 toIndex (fromIndex > toIndex),则subList()方法会抛出IllegalArgumentException错误。

//If fromIndex > toIndex
ArrayList new_list2 = new ArrayList(list.subList(5, 3));
System.out.println("Portion of the list: "+new_list2);
 
Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(5) > toIndex(3)

结论

在本文中,我们讨论了subList方法及其使用方法。subList()方法消除了对显式范围操作的需要(这是数组通常存在的一种操作)。最重要的是要记住 subList 方法不返回新实例,而是返回一个引用原始列表的视图。因此,在同一个列表上过度使用subList方法会导致线程卡在 Java 应用程序中。
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION