Java中的List接口提供了一种存储有序集合的方法。它是 收集 .它是一个有序的对象集合,可以在其中存储重复的值。因为列表保留了插入顺序,所以它允许对元素进行位置访问和插入。
列表接口可以在java中找到。util包并继承集合接口。它是ListIterator接口的工厂。通过ListIterator,我们可以向前和向后迭代列表。列表接口的实现类有ArrayList、LinkedList、Stack和Vector。ArrayList和LinkedList在Java编程中被广泛使用。自从Java5以来,Vector类就被弃用了。
宣言: 列表接口声明为:
public interface List<E> extends Collection<E> ;
让我们详细介绍一下如何在List类中创建对象或实例。 自从 列表 是一个 界面 ,无法创建类型列表的对象。我们总是需要一个实现这个的类 列表 以创建一个对象。而且,在引入 仿制药 在Java1.5中,可以限制可以存储在列表中的对象的类型。就像其他几个由用户定义的“类”实现的用户定义的“接口”一样, 列表 是一个“接口”,由 ArrayList 类,在 JAVAutil 包裹
语法: 这种类型的安全列表可以定义为:
List<Obj> list = new ArrayList<Obj> ();
注: Obj是要存储在列表中的对象的类型
例子:
JAVA
// Java program to Demonstrate List Interface // Importing all utility classes import java.util.*; // Main class // ListDemo class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of List interface // implemented by the ArrayList class List<Integer> l1 = new ArrayList<Integer>(); // Adding elements to object of List interface // Custom inputs l1.add( 0 , 1 ); l1.add( 1 , 2 ); // Print the elements inside the object System.out.println(l1); // Now creating another object of the List // interface implemented ArrayList class // Declaring object of integer type List<Integer> l2 = new ArrayList<Integer>(); // Again adding elements to object of List interface // Custom inputs l2.add( 1 ); l2.add( 2 ); l2.add( 3 ); // Will add list l2 from 1 index l1.addAll( 1 , l2); System.out.println(l1); // Removes element from index 1 l1.remove( 1 ); // Printing the updated List 1 System.out.println(l1); // Prints element at index 3 in list 1 // using get() method System.out.println(l1.get( 3 )); // Replace 0th element with 5 // in List 1 l1.set( 0 , 5 ); // Again printing the updated List 1 System.out.println(l1); } } |
[1, 2][1, 1, 2, 3, 2][1, 2, 3, 2]2[5, 2, 3, 2]
现在,让我们使用列表界面执行各种操作,以便更好地理解它们。我们将讨论下面列出的以下操作,以及稍后通过干净的java代码实现的操作。
列表界面中的操作
因为List是一个接口,所以它只能与实现该接口的类一起使用。现在,让我们看看如何在列表中执行一些常用的操作。
- 操作1: 使用add()方法向List类添加元素
- 操作2: 使用set()方法更新List类中的元素
- 操作3: 使用remove()方法删除元素
现在让我们单独讨论这些操作,并在代码中实现它们,以便更好地掌握它们。
操作1: 使用添加元素到列表类 add()方法
为了向列表中添加元素,我们可以使用 add()方法 。此方法被重载以基于不同参数执行多个操作。
参数: 它需要两个参数,即:
- 添加(对象): 此方法用于在列表末尾添加元素。
- 添加(int索引,对象): 此方法用于在列表中的特定索引处添加元素
例子:
JAVA
// Java Program to Add Elements to a List // Importing all utility classes import java.util.*; // Main class class GFG { // Main driver method public static void main(String args[]) { // Creating an object of List interface, // implemented by ArrayList class List<String> al = new ArrayList<>(); // Adding elements to object of List interface // Custom elements al.add( "Geeks" ); al.add( "Geeks" ); al.add( 1 , "For" ); // Print all the elements inside the // List interface object System.out.println(al); } } |
[Geeks, For, Geeks]
操作2: 更新元素
添加元素后,如果我们希望更改元素,可以使用 set() 方法 .由于列表是索引的,所以我们希望更改的元素由该元素的索引引用。因此,该方法采用一个索引和需要插入该索引的更新元素。
例子:
JAVA
// Java Program to Update Elements in a List // Importing utility classes import java.util.*; // Main class class GFG { // Main driver method public static void main(String args[]) { // Creating an object of List interface List<String> al = new ArrayList<>(); // Adding elements to object of List class al.add( "Geeks" ); al.add( "Geeks" ); al.add( 1 , "Geeks" ); // Display theinitial elements in List System.out.println( "Initial ArrayList " + al); // Setting (updating) element at 1st index // using set() method al.set( 1 , "For" ); // Print and display the updated List System.out.println( "Updated ArrayList " + al); } } |
Initial ArrayList [Geeks, Geeks, Geeks]Updated ArrayList [Geeks, For, Geeks]
操作3: 移除元素
为了从列表中删除元素,我们可以使用 remove()方法 。此方法被重载以基于不同参数执行多个操作。他们是:
参数:
- 移除(对象): 此方法仅用于从列表中删除对象。如果有多个这样的对象,则删除该对象的第一个引用。
- 删除(int索引): 由于列表是索引的,所以该方法采用一个整数值,该整数值只需删除列表中特定索引处的元素。移除元素后,所有元素都会向左移动以填充空间,对象的索引也会更新。
例子:
JAVA
// Java Program to Remove Elements from a List // Importing List and ArrayList classes // from java.util package import java.util.ArrayList; import java.util.List; // Main class class GFG { // Main driver method public static void main(String args[]) { // Creating List class object List<String> al = new ArrayList<>(); // Adding elements to the object // Custom inputs al.add( "Geeks" ); al.add( "Geeks" ); // Adding For at 1st indexes al.add( 1 , "For" ); // Print the initialArrayList System.out.println( "Initial ArrayList " + al); // Now remove element from the above list // present at 1st index al.remove( 1 ); // Print the List after removal of element System.out.println( "After the Index Removal " + al); // Now remove the current object from the updated // List al.remove( "Geeks" ); // Finally print the updated List now System.out.println( "After the Object Removal " + al); } } |
Initial ArrayList [Geeks, For, Geeks]After the Index Removal [Geeks, Geeks]After the Object Removal [Geeks]
遍历列表
到目前为止,我们的输入规模非常小,我们正在为每个实体手动操作。现在,让我们讨论各种方法,通过这些方法,我们可以迭代列表,使它们适用于更大的样本集。
方法: 有多种方法可以循环浏览列表。最著名的方法是使用基本的 循环 结合 get()方法 获取特定索引处的元素和 先进的循环 .
例子:
JAVA
// Java program to Iterate the Elements // in an ArrayList // Importing java utility classes import java.util.*; // Main class public class GFG { // main driver method public static void main(String args[]) { // Creating an empty Arraylist of string type List<String> al = new ArrayList<>(); // Adding elements to above object of ArrayList al.add( "Geeks" ); al.add( "Geeks" ); // Adding element at specified position // inside list object al.add( 1 , "For" ); // Using for loop for iteration for ( int i = 0 ; i < al.size(); i++) { // Using get() method to // access particular element System.out.print(al.get(i) + " " ); } // New line for better readability System.out.println(); // Using for-each loop for iteration for (String str : al) // Printing all the elements // which was inside object System.out.print(str + " " ); } } |
Geeks For Geeks Geeks For Geeks
列表界面的方法
由于不同类型列表背后的主要概念是相同的,列表界面包含以下方法:
方法 |
描述 |
---|---|
添加(int索引,元素) | 此方法用于在列表中的特定索引处添加元素。当传递单个参数时,它只需将元素添加到列表的末尾。 |
addAll(整数索引、集合) | 此方法用于将给定集合中的所有元素添加到列表中。当传递单个参数时,它会将给定集合的所有元素添加到列表的末尾。 |
大小() | 此方法用于返回列表的大小。 |
清除() | 此方法用于删除列表中的所有元素。但是,创建的列表的引用仍然被存储。 |
删除(整型索引) | 此方法从指定的索引中删除元素。它会将后续元素(如果有)向左移动,并将其索引减少1。 |
移除(元素) | 此方法用于删除列表中给定元素的第一个匹配项。 |
get(int索引) | 此方法返回指定索引处的元素。 |
集合(int索引,元素) | 此方法使用新元素替换给定索引中的元素。此函数返回刚被新元素替换的元素。 |
indexOf(元素) | 此方法返回给定元素或元素的第一次出现 -1 如果列表中不存在该元素。 |
lastIndexOf(元素) | 此方法返回给定元素或元素的最后一次出现 -1 如果列表中不存在该元素。 |
等于(元素) | 此方法用于比较给定元素与列表元素的相等性。 |
hashCode() | 此方法用于返回给定列表的hashcode值。 |
isEmpty() | 此方法用于检查列表是否为空。如果列表为空,则返回true,否则返回false。 |
包含(元素) | 此方法用于检查列表是否包含给定元素。如果列表包含该元素,则返回true。 |
containsAll(收集) | 此方法用于检查列表是否包含所有元素集合。 |
排序(比较器comp) | 此方法用于根据给定的值对列表中的元素进行排序 比较器 . |
Java列表与集合
列表接口和集合接口都继承了集合接口。然而,它们之间存在一些差异。
列表 | 设置 |
---|---|
列表是一个有序的序列。 | 布景是一个无序的序列。 |
列表允许重复元素 | 集合不允许重复元素。 |
可以通过位置访问元素。 | 不允许位置访问元素。 |
可以存储多个空元素。 | null元素只能存储一次。 |
列表实现包括ArrayList、LinkedList、Vector和Stack | Set实现是HashSet、LinkedHashSet。 |
与列表接口关联的类
现在,让我们讨论实现列表接口的类,首先参考下面的图形表示,以便更好地理解列表接口。详情如下:
摘要列表 , CopyOnWriteArrayList ,以及 摘要顺序表 是实现列表接口的类。上述每个类中都实现了单独的功能。详情如下:
- 摘要列表: 这个类用于实现一个不可修改的列表,对于这个列表,只需要扩展这个AbstractList类并实现 得到() 还有 大小() 方法。
- CopyOnWriteArrayList: 此类实现列表接口。它是的增强版 ArrayList 所有修改(添加、设置、删除等)都是通过制作列表的新副本来实现的。
- 摘要顺序列表: 这个类实现了 收集接口 还有AbstractCollection类。这个类用于实现一个不可修改的列表,对于这个列表,只需要扩展这个AbstractList类并实现 得到() 还有 大小() 方法。
我们将以这种方式进行。
- ArrayList
- 矢量
- 堆栈
- 链表
让我们按顺序讨论它们,并实现它们,以了解使用列表接口的类的工作方式。
类别1:ArrayList
ArrayList 在集合框架中实现的类为我们提供了Java中的动态数组。不过,它可能比标准数组慢,但在需要对数组进行大量操作的程序中可能会有所帮助。让我们看看如何使用这个类创建列表对象。
例子:
JAVA
// Java program to demonstrate the // creation of list object using the // ArrayList class import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Size of ArrayList int n = 5 ; // Declaring the List with initial size n List<Integer> arrli = new ArrayList<Integer>(n); // Appending the new elements // at the end of the list for ( int i = 1 ; i <= n; i++) arrli.add(i); // Printing elements System.out.println(arrli); // Remove element at index 3 arrli.remove( 3 ); // Displaying the list after deletion System.out.println(arrli); // Printing elements one by one for ( int i = 0 ; i < arrli.size(); i++) System.out.print(arrli.get(i) + " " ); } } |
[1, 2, 3, 4, 5][1, 2, 3, 5]1 2 3 5
第2类:向量
Vector是一个在集合框架中实现的类,它实现了一个可扩展的对象数组。Vector实现了一个动态数组,这意味着它可以根据需要增长或收缩。与数组一样,它包含可以使用整数索引访问的组件。向量基本上属于遗留类,但现在它与集合完全兼容。让我们看看如何使用这个类创建列表对象。
例子:
JAVA
// Java program to demonstrate the // creation of list object using the // Vector class import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Size of the vector int n = 5 ; // Declaring the List with initial size n List<Integer> v = new Vector<Integer>(n); // Appending the new elements // at the end of the list for ( int i = 1 ; i <= n; i++) v.add(i); // Printing elements System.out.println(v); // Remove element at index 3 v.remove( 3 ); // Displaying the list after deletion System.out.println(v); // Printing elements one by one for ( int i = 0 ; i < v.size(); i++) System.out.print(v.get(i) + " " ); } } |
[1, 2, 3, 4, 5][1, 2, 3, 5]1 2 3 5
第三类:堆栈
Stack是一个在集合框架中实现的类,它扩展了向量类模型并实现了 堆栈数据结构 .这门课基于后进先出的基本原则。除了基本的push和pop操作外,这个类还提供了另外三个函数:empty、search和peek。让我们看看如何使用这个类创建列表对象。
例子:
JAVA
// Java program to demonstrate the // creation of list object using the // Stack class import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Size of the stack int n = 5 ; // Declaring the List List<Integer> s = new Stack<Integer>(); // Appending the new elements // at the end of the list for ( int i = 1 ; i <= n; i++) s.add(i); // Printing elements System.out.println(s); // Remove element at index 3 s.remove( 3 ); // Displaying the list after deletion System.out.println(s); // Printing elements one by one for ( int i = 0 ; i < s.size(); i++) System.out.print(s.get(i) + " " ); } } |
[1, 2, 3, 4, 5][1, 2, 3, 5]1 2 3 5
第4类:LinkedList
LinkedList是一个在集合框架中实现的类,集合框架本质上实现了 链表数据结构 它是一种线性数据结构,其中元素不存储在连续位置,每个元素都是一个单独的对象,具有数据部分和地址部分。元素使用指针和地址链接。每个元素称为一个节点。由于插入和删除的动态性和易用性,它们比数组更受欢迎。让我们看看如何使用这个类创建列表对象。
例子:
JAVA
// Java program to demonstrate the // creation of list object using the // LinkedList class import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // Size of the LinkedList int n = 5 ; // Declaring the List with initial size n List<Integer> ll = new LinkedList<Integer>(); // Appending the new elements // at the end of the list for ( int i = 1 ; i <= n; i++) ll.add(i); // Printing elements System.out.println(ll); // Remove element at index 3 ll.remove( 3 ); // Displaying the list after deletion System.out.println(ll); // Printing elements one by one for ( int i = 0 ; i < ll.size(); i++) System.out.print(ll.get(i) + " " ); } } |
[1, 2, 3, 4, 5][1, 2, 3, 5]1 2 3 5