如何在Java中的列表中查找子列表?
Java中的 List包含基于索引的方法。这使我们能够维护订单集合。因此,这使我们能够搜索、插入、删除甚至更新元素。这使我们能够存储列表中已经存在的相同元素的多个副本。此外,还允许空元素成为 List 的一部分。
我们通过它们称为接口的索引号访问列表,我们不会在这里讨论。
列表类型
- 数组列表
- 链表
- 堆
- 向量
ArrayList 用于已知要插入的元素的地方,因为一旦我们声明了 ArrayList 就没有灵活性,但经常使用,因为对元素的操作要快得多,而且 Arraylist 的好处是我们可以通过 ArrayL 直接访问元素是t 接口.
ArrayList 的语法:
ArrayList cars = new ArrayList();
如果我们想要一个没有大小限制的灵活列表并且对元素的操作相当慢,那么LinkedList比 Arraylist 更受欢迎。
LinkedList extends AbstractList implements List, Deque ;
Vector方法类似于 Arraylist,只是 Vector 比 ArrayList 有优势,因为向量中的所有元素都是同步的,并且仅在进行多线程应用程序时才有用。因此,实际上向量类不再被更频繁地使用。
Vector object= new vector(datatype parameter1, datatype parameter2, ...., datatype parameterN)
子列表是列表的一部分
Java.util.ArrayList类的subList()方法用于返回此列表中指定的 fromIndex(包含)和 toIndex(不包含)之间的部分的视图。
返回列表受此列表支持,因此返回列表中的非结构性更改会反映在此列表中,反之亦然。返回的列表支持所有可选的列表操作。
句法:
public List subList(int fromIndex, int toIndex)
参数:此方法将以下参数作为参数。
- fromIndex: subList的低端(含)
- toIndex: subList的高端(独占)
返回类型:此列表中指定范围的视图。异常:此方法抛出以下异常。
- IndexOutOfBoundsException – 如果端点索引值超出范围(fromIndex 大小)
- IllegalArgumentException – 如果端点索引乱序(fromIndex > toIndex)
示例 1:
Java
// Java Program to find
// Sublist in a List
import java.util.*;
public class GFG1 {
// Main Method
public static void main(String[] argv) throws Exception
{
// Try block for exception
try {
ArrayList
arrlist = new ArrayList();
// Populating arrlist1
arrlist.add(1);
arrlist.add(4);
arrlist.add(9);
arrlist.add(25);
arrlist.add(36);
// 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 block for exception
catch (IndexOutOfBoundsException e)
{
System.out.println("Exception thrown : " + e);
}
// Catch block for exception
catch (IllegalArgumentException e)
{
System.out.println("Exception thrown : " + e);
}
}
}
Java
// Java program to find
// sublist in a List
import java.util.*;
public class GFG1
{
// Main Method
public static void main(String[] argv) throws Exception
{
// Exception try-catch block
try {
ArrayList arrlist = new ArrayList();
// Populating arrlist1
arrlist.add("Example");
arrlist.add("in");
arrlist.add("Geeks");
arrlist.add("for");
arrlist.add("Geeks");
// print arrlist
System.out.println("Original arrlist: "
+ arrlist);
// Getting the subList
// using subList() method
List arrlist2 = arrlist.subList(2, 5);
// print the subList
System.out.println("Sublist of arrlist: "
+ arrlist2);
}
// Exception try-catch block
catch (IndexOutOfBoundsException e)
{
System.out.println("Exception thrown : " + e);
}
// Exception try-catch block
catch (IllegalArgumentException e)
{
System.out.println("Exception thrown : " + e);
}
}
}
Java
// Java program to demonstrate
// subList() method
// for IllegalArgumentException
import java.util.*;
public class GFG1
{
// Main Method
public static void main(String[] argv) throws Exception
{
// Exception try-catch block
try {
ArrayList arrlist = new ArrayList();
// Populating arrlist1
arrlist.add("Example");
arrlist.add("in");
arrlist.add("Geeks");
arrlist.add("for");
arrlist.add("Geeks");
// 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(9, 3);
// print the subList
System.out.println("Sublist of arrlist: "
+ arrlist2);
}
// Exception try-catch block
catch (IndexOutOfBoundsException e)
{
System.out.println("Exception thrown: " + e);
}
// Exception try-catch block
catch (IllegalArgumentException e)
{
System.out.println("Exception thrown: " + e);
}
}
}
Java
// Java program to demonstrate subList()
// method for IndexOutOfBoundsException
import java.util.*;
public class GFG1
{
// Main Method
public static void main(String[] argv) throws Exception
{
// Exception try-catch block
try {
ArrayList arrlist = new ArrayList();
// Populating arrlist1
arrlist.add(1);
arrlist.add(4);
arrlist.add(9);
arrlist.add(25);
arrlist.add(36);
// 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);
}
// Exception try-catch block
catch (IndexOutOfBoundsException e)
{
System.out.println("Exception thrown : " + e);
}
// Exception try-catch block
catch (IllegalArgumentException e)
{
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Original arrlist: [1, 4, 9, 25, 36]
Sublist of arrlist: [9, 25]
示例 2:
Java
// Java program to find
// sublist in a List
import java.util.*;
public class GFG1
{
// Main Method
public static void main(String[] argv) throws Exception
{
// Exception try-catch block
try {
ArrayList arrlist = new ArrayList();
// Populating arrlist1
arrlist.add("Example");
arrlist.add("in");
arrlist.add("Geeks");
arrlist.add("for");
arrlist.add("Geeks");
// print arrlist
System.out.println("Original arrlist: "
+ arrlist);
// Getting the subList
// using subList() method
List arrlist2 = arrlist.subList(2, 5);
// print the subList
System.out.println("Sublist of arrlist: "
+ arrlist2);
}
// Exception try-catch block
catch (IndexOutOfBoundsException e)
{
System.out.println("Exception thrown : " + e);
}
// Exception try-catch block
catch (IllegalArgumentException e)
{
System.out.println("Exception thrown : " + e);
}
}
}
输出 :
Original arrlist: [Example, in, Geeks, for, Geeks]
Sublist of arrlist: [Geeks, for, Geeks]
示例 3:对于 IllegalArgumentException
Java
// Java program to demonstrate
// subList() method
// for IllegalArgumentException
import java.util.*;
public class GFG1
{
// Main Method
public static void main(String[] argv) throws Exception
{
// Exception try-catch block
try {
ArrayList arrlist = new ArrayList();
// Populating arrlist1
arrlist.add("Example");
arrlist.add("in");
arrlist.add("Geeks");
arrlist.add("for");
arrlist.add("Geeks");
// 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(9, 3);
// print the subList
System.out.println("Sublist of arrlist: "
+ arrlist2);
}
// Exception try-catch block
catch (IndexOutOfBoundsException e)
{
System.out.println("Exception thrown: " + e);
}
// Exception try-catch block
catch (IllegalArgumentException e)
{
System.out.println("Exception thrown: " + e);
}
}
}
输出:
Original arrlist: [Example, in, Geeks, for, Geeks]
Endpoint indices are out of order (fromIndex > toIndex)
Exception thrown: java.lang.IllegalArgumentException: fromIndex(9) > toIndex(3)
示例 4:对于 IndexOutOfBoundsException
Java
// Java program to demonstrate subList()
// method for IndexOutOfBoundsException
import java.util.*;
public class GFG1
{
// Main Method
public static void main(String[] argv) throws Exception
{
// Exception try-catch block
try {
ArrayList arrlist = new ArrayList();
// Populating arrlist1
arrlist.add(1);
arrlist.add(4);
arrlist.add(9);
arrlist.add(25);
arrlist.add(36);
// 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);
}
// Exception try-catch block
catch (IndexOutOfBoundsException e)
{
System.out.println("Exception thrown : " + e);
}
// Exception try-catch block
catch (IllegalArgumentException e)
{
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Original arrlist: [1, 4, 9, 25, 36]
End index value is out of range
Exception thrown : java.lang.IndexOutOfBoundsException: toIndex = 7