如果我有多个数组并且我想在所有数组中搜索,如何在 java 中搜索字符串?

Posted

技术标签:

【中文标题】如果我有多个数组并且我想在所有数组中搜索,如何在 java 中搜索字符串?【英文标题】:How to search for a String in java if I have more than one array and I want to search in all of them? 【发布时间】:2018-05-26 18:54:55 【问题描述】:
import java.util.Scanner;
public class Problem1 

    public static void main(String args[]) 
    
        Scanner input =new Scanner(System.in);
        String search =input.nextLine();
        String arr1[] = "Ranitidine","Seclo","Aceclofenac","Finix","Stemetile";
        String arr2[] = "Raine","Seclo","Aceofenac","Finix","temetile";
        String arr3[] = "Ranitie","Seclo","Aclofenac","Fix","Stile";
        String arr4[] = "Ranitide","Sclo","Acecofenac","Finx","Stemile";
    

【问题讨论】:

你试过在一个数组中搜索吗?如果你知道如何在一个数组中搜索,为什么不只搜索所有数组呢? Stream.of(arr1, arr2, arr3, arr4).flatMap(Stream::of).anyMatch(Predicate.isEqual(search)) 呵呵@shmosel 它很漂亮,但也不会帮助初学者:) 我的意思是他不会理解 【参考方案1】:

这样试试

public static void main(String[] args)
    
        String key = "Jones";
        String[ ] list = new String [100];   // instantiate the array
        for ( int i=0; i<100; i++)     // fill the array
             list [ i ]=Console.readLine("Enter name:  ");
        int count = search (list, key);    // invoke the method
        System.out.println("Count = " + count);
    
    public static int search(String [ ] list, String key) //method to find "Jones"
    
         int i, count = 0;
         for( i = 0; i< list.length; i++)
         
              if (list [ i ].equals( key ))
                 count = count+1;
         
         return (count);
     

/////////////........

public static void main(String[] args) 

        // adding element in search string array
        String[] searchArray = "object", "search", "word";

        boolean searchArrayResult = false;

        // This block search all string array
        // if string find in array it will break from 
        // string array loop and return true value.
        for (String str : searchArray)
            searchArrayResult = str.contains("wor");
            if(searchArrayResult)
                break;
            
        
        System.out.println("Search string Array result :"+searchArrayResult);
    

【讨论】:

以上是关于如果我有多个数组并且我想在所有数组中搜索,如何在 java 中搜索字符串?的主要内容,如果未能解决你的问题,请参考以下文章

Swift3 iOS - 如何在 URL 数组中搜索 URL [URL?]

如何在 NSMutableArray 上添加观察者?

Firestore 搜索数组包含多个值

如何在字符串中查找字符串数组?

在多个列中搜索

如何在 Iphone SDK 中基于单个数组对多个数组进行排序