📜  Java中的LinkedList contains()方法

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

Java中的LinkedList contains()方法

Java.util.LinkedList.contains() 方法用于检查元素是否存在于 LinkedList 中。它将元素作为参数,如果元素存在于列表中,则返回 True。

句法:

LinkedList.contains(Object element)

参数:参数元素的类型为 LinkedList。该参数引用列表中需要检查其出现的元素。

返回值:如果元素存在于 LinkedList 中,则该方法返回 True,否则返回 False。

下面的程序说明了Java.util.LinkedList.contains() 方法:

Java
// Java code to illustrate boolean contains()
import java.io.*;
import java.util.LinkedList;
 
public class LinkedListDemo {
   public static void main(String args[]) {
 
      // Creating an empty LinkedList
      LinkedList list = new LinkedList();
 
      // Use add() method to add elements in the list
      list.add("Geeks");
      list.add("for");
      list.add("Geeks");
      list.add("10");
      list.add("20");
 
      // Output the list
      System.out.println("LinkedList:" + list);
 
      // Check if the list contains "Hello"
      System.out.println("\nDoes the List contains 'Hello': "
                                      + list.contains("Hello"));
 
      // Check if the list contains "20"
      System.out.println("Does the List contains '20': "
                                         + list.contains("20"));
       
      // Check if the list contains "Geeks"
      System.out.println("Does the List contains 'Geeks': "
                                      + list.contains("Geeks"));
 
   }
}


输出:
LinkedList:[Geeks, for, Geeks, 10, 20]

Does the List contains 'Hello': false
Does the List contains '20': true
Does the List contains 'Geeks': true