简体   繁体   English

如何使用 (.contains) 从数组列表中查找特定字符串

[英]How do I find a specific string from an array list using (.contains)

I need to find a specific string (letter) from an array list.我需要从数组列表中找到一个特定的字符串(字母)。 I have used the.add function to add a list I have created previously into my array.我使用了.add function 将我之前创建的列表添加到我的数组中。 How do I (using my array list) find a specific word with a particular letter that the user has typed in.我如何(使用我的数组列表)找到具有用户输入的特定字母的特定单词。

import java.util.*;

public class Library {  
    
    public static void main (String[] args)
    {
        
        
        ArrayList<Book> bookList = new ArrayList<Book>();
        ArrayList<libraryMembers> memberList = new ArrayList<libraryMembers>();
        
        
    String userChoice;
    
    {   
        
        //Using previous class and assigning values to ID, Phone, Name, and Surname
        libraryMembers lm1 = new libraryMembers (1111, 3234231, "Glock", "John");
        libraryMembers lm2 = new libraryMembers (1112, 3234231, "Ackerman", "Mikasa");
        libraryMembers lm3 = new libraryMembers (1113, 341231, "Crab", "Armin");
        libraryMembers lm4 = new libraryMembers (1114, 3423431, "Bacony", "Conny");
        libraryMembers lm5 = new libraryMembers (1115, 3423431, "Jaeger", "Erwin");
        libraryMembers lm6 = new libraryMembers (1116, 3423431, "Jaeger", "Zeke");
        
        //Printing member information
        lm1.informationPrint();
        lm2.informationPrint();
        lm3.informationPrint();
        lm4.informationPrint();
        lm5.informationPrint();
        lm6.informationPrint();
            
        
        memberList.add(lm1);
        memberList.add(lm2);
        memberList.add(lm3);
        memberList.add(lm4);
        memberList.add(lm5);
        memberList.add(lm6);

From this array that I have put the information in I want the user to input a letter and for the program to output all the names and surnames containing that specific letter.从这个数组中,我希望用户输入一个字母,并为程序 output 输入包含该特定字母的所有姓名和姓氏。

List.contains() only tells you if the list contains a certain object based on equals . List.contains()仅根据equals告诉您列表是否包含某个 object 。 And contains would expect an argument of the List type (in your case LibraryMembers ).并且contains会期望List type的参数(在您的情况下为LibraryMembers )。

But you can achieve the desired result as follows.但是您可以按如下方式获得所需的结果。 This presumes that you have getters and setters for first and last names.这假定您有名字和姓氏的 getter 和 setter。 It works by streaming the memberlist and searching each name for the desired letter.它通过流式传输成员列表并在每个名称中搜索所需字母来工作。 The search is case significant but can be changed using toLowerCase on the names and always using lower case on the required character or substring to find.搜索是区分大小写的,但可以在名称上使用toLowerCase进行更改,并且始终在所需字符上使用小写字母或 substring 进行查找。

boolean result = memberList.stream()
              .anyMatch(lm->lm.getLastName().contains("G") || 
                            lm.getFirstName().contains("G"));

Then do your test on result.然后对结果进行测试。

If you want to recover all the entries that match, you can do this.如果你想恢复所有匹配的条目,你可以这样做。

List<LibraryMembers> results = memberList.stream()
     .filter(lm->lm.getLastName().contains("G") || 
             lm.getFirstName().contains("G"))
      .collect(Collectors.toList());

if (results.size() > 0) {
   results.forEach(System.out::println); // requires toString override
                                         // in LibraryMember class.
} else {
   System.out.println("Request not found");
}


I want the program to look something like this:我希望程序看起来像这样:

System.out.println("The program will allow the user to search for a string by typing a 
letter")

userAnswer = scan.next();

boolean userAnswer = memberList.contains("G"); 
    
        if (userAnswer)
            System.out.println("The list contains Glock,");
        else
            System.out.println("The list does not contain any words with that letter");
        

So that the information comes from:所以信息来自:

import java.util.*;

public class Library {

    



public static void main (String[] args)
{
    
    
    ArrayList<Book> bookList = new ArrayList<Book>();
    ArrayList<libraryMembers> memberList = new ArrayList<libraryMembers>();
    
    
String userChoice;

{
  
    
    
    
    
    //Using previous class and assigning values to ID, Phone, Name, and Surname
    libraryMembers lm1 = new libraryMembers (1111, 3234231, "Glock", "John");
    libraryMembers lm2 = new libraryMembers (1112, 3234231, "Ackerman", "Mikasa");
    libraryMembers lm3 = new libraryMembers (1113, 341231, "Crab", "Armin");
    libraryMembers lm4 = new libraryMembers (1114, 3423431, "Bacony", "Conny");
    libraryMembers lm5 = new libraryMembers (1115, 3423431, "Jaeger", "Erwin");
    libraryMembers lm6 = new libraryMembers (1116, 3423431, "Jaeger", "Zeke");
    
    //Printing member information
    lm1.informationPrint();
    lm2.informationPrint();
    lm3.informationPrint();
    lm4.informationPrint();
    lm5.informationPrint();
    lm6.informationPrint();

// The line above just prints out the information // 上面一行只是打印出信息

    memberList.add(lm1);
    memberList.add(lm2);
    memberList.add(lm3);
    memberList.add(lm4);
    memberList.add(lm5);
    memberList.add(lm6);
    
// Using this line I have added my lists to the array

It does not need to be case sensitive.它不需要区分大小写。

I've tried something like this:我试过这样的事情:

public static void main (String[] args) {公共 static 无效主(字符串 [] 参数){

    ArrayList<String> memberList = new ArrayList<String>();

memberList.add("Glock, John, Ackerman, Mikasa, Crab, Armin, Bacony, Conny, 
Jaeger, Erwin, Jaeger, Zeke");

boolean userAnswer = memberList.contains("G");
    
    if (userAnswer)
        System.out.println("The list contains Glock");
    else
        System.out.println("The list does not containg string with letter g");
        

But the only way I can find something is by typing out the full string not the letter.但我能找到的唯一方法是输入完整的字符串而不是字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM