如何在Java中获取 LinkedList 的子列表?
链表是Java.util 包中集合框架的一部分。这个类是 LinkedList 数据结构的一个实现,它是一种线性数据结构,其中元素不存储在连续的位置,每个元素都是一个单独的对象,具有数据部分和地址部分。
给定 LinkedList 中存在的元素列表,我们需要找到给定范围的子列表的元素。
例子 :
The elements of the LinkedList are: [3, 5, 2, 1, 7, 8]
Enter the start and end of the required sublist:
start position -> 1
end position -> 4
The required SubList is: [5, 2, 1]
where start position is inclusive and the end position is exclusive
方法:使用util 包的 LinkedList 类中存在的默认subList()方法。
这个很简单,也很直接。我们基本上使用Java.util.LinkedList.subList() 。
句法:
public List subList(int fromIndex, int toIndex)
参数:此方法将以下参数作为参数。
- fromIndex –子列表的低端点(包括)
- toIndex – subList 的高端(独占)
返回值:此方法返回此列表中指定范围的视图。
算法 :
- 在 LinkedList 中输入元素或获取 LinkedList。
- 输入要查找的 subList 的范围的开始(包括,基于 0)。
- 输入范围的结尾(不包括,从 0 开始)。
- 使用 start 和 end 作为 subList() 方法的参数,并将其分配给新列表以存储此子列表。
示例 1:
Java
// Java program to get Sublist of LinkedList
import java.util.LinkedList;
import java.util.List;
public class subLinkedList {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
// adding elements
list.add("apple");
list.add("mango");
list.add("peach");
list.add("guava");
list.add("banana");
list.add("lichi");
// printing initial elements
System.out.println(
"The elements of the LinkedList are: " + list);
System.out.println(
"Enter the start and end of the required sublist: ");
// entering start and end indices
int start = 2, end = 5;
List sublist = list.subList(start, end);
System.out.println("The required SubList is: "
+ sublist);
}
}
Java
// Java program to get the sublist from
// Linked List of Linked lists
import java.util.LinkedList;
import java.util.List;
public class subLinkedList {
public static void main(String[] args)
{
// creating linkedlist of linkedlists
LinkedList > list
= new LinkedList<>();
// creating lists
LinkedList list1 = new LinkedList<>();
list1.add(8);
list1.add(0);
LinkedList list2 = new LinkedList<>();
list2.add(10);
list2.add(4);
list2.add(3);
list2.add(5);
LinkedList list3 = new LinkedList<>();
list3.add(1);
list3.add(2);
list3.add(9);
// adding linkedlists to main linkedlist
list.add(list1);
list.add(list2);
list.add(list3);
// printing initial lists
System.out.println(
"The elements of the LinkedList are: " + list);
System.out.println(
"Enter the start and end of the required sublists: ");
// entering start and end indices
int start = 1, end = 3;
List sublist = list.subList(start, end);
System.out.println("The required SubList is: "
+ sublist);
}
}
输出
The elements of the LinkedList are: [apple, mango, peach, guava, banana, lichi]
Enter the start and end of the required sublist:
The required SubList is: [peach, guava, banana]
示例2:从LinkedLists 的Linked List 中获取子列表。
Java
// Java program to get the sublist from
// Linked List of Linked lists
import java.util.LinkedList;
import java.util.List;
public class subLinkedList {
public static void main(String[] args)
{
// creating linkedlist of linkedlists
LinkedList > list
= new LinkedList<>();
// creating lists
LinkedList list1 = new LinkedList<>();
list1.add(8);
list1.add(0);
LinkedList list2 = new LinkedList<>();
list2.add(10);
list2.add(4);
list2.add(3);
list2.add(5);
LinkedList list3 = new LinkedList<>();
list3.add(1);
list3.add(2);
list3.add(9);
// adding linkedlists to main linkedlist
list.add(list1);
list.add(list2);
list.add(list3);
// printing initial lists
System.out.println(
"The elements of the LinkedList are: " + list);
System.out.println(
"Enter the start and end of the required sublists: ");
// entering start and end indices
int start = 1, end = 3;
List sublist = list.subList(start, end);
System.out.println("The required SubList is: "
+ sublist);
}
}
输出
The elements of the LinkedList are: [[8, 0], [10, 4, 3, 5], [1, 2, 9]]
Enter the start and end of the required sublists:
The required SubList is: [[10, 4, 3, 5], [1, 2, 9]]