簡體   English   中英

如何通過char數組搜索String數組?

[英]How to search String array through char array?

我想構建一個不使用字典庫的簡單字典搜索程序。 如果數組中的字符串等於char數組,我想搜索字符串數組。 它應該打印字符串及其索引號。 如果有兩個與char匹配的字符串,則應打印第一個字符串,並保留后一個字符串

.eg字符串array [“ fine”,“ rest”,“ door”,“ shine”] char字符['t','r','e','s']。 答案應該是索引1處的“ rest”。如果重復了String rest,那么它應該只打印第一個。

我試圖比較字符串數組和char數組,但是它返回匹配char數組的所有字符串單詞。

String strArray[]=new String[4];
char chrArray[]=new char[4]; 
String value="";
char compare;
System.out.println("Enter the words :");
    for(int i=0;i<strArray.length;i++){
    strArray[i]=input.next();

    } 
    System.out.println("Enter the Characters :");
    for (int i = 0; i < chrArray.length; i++) {
        chrArray[i]=input.next().charAt(0);

    }

    for (int i = 0; i < strArray.length; i++) {
            if(strArray[i].length()==chrArray.length){
                 if(""+strArray[i]!=value){
               value="";
            }
        for (int j = 0; j < strArray[i].length(); j++) {

            for (int k = 0; k < chrArray.length; k++) {


                if(strArray[i].charAt(j)==chrArray[k]){
                value=value+strArray[i].charAt(j);   

                }
            }
            }
        }

}
        System.out.println(value);

輸出應該是來自數組的字符串,該字符串等於char數組。

您可以對char數組進行排序,然后使用Arrays.equal對其進行比較。 通過對char數組進行排序,將不需要為循環使用2。

import java.util.Arrays;
import java.util.Scanner;

public class Bug {

        public static void main(String[] args) {
            String strArray[]=new String[4];
            char chrArray[]=new char[4];
            Scanner input = new Scanner(System.in);
            System.out.println("Enter the words :");
            for(int i=0;i<strArray.length;i++){
                strArray[i]=input.next();

            }
            System.out.println("Enter the Characters :");
            for (int i = 0; i < chrArray.length; i++) {
                chrArray[i]=input.next().charAt(0);

            }
            Arrays.sort(chrArray);

            for (int i = 0; i < strArray.length; i++) {
                char[] x = strArray[i].toCharArray();
                Arrays.sort(x);
                if(Arrays.equals(chrArray, x))
                {
                    System.out.println(strArray[i]);
                    break;
                }
            }
        }

 }

您還可以創建Map<Character, Integer>來存儲單詞和字符的計數,並將每個單詞的字符計數與字符計數進行比較。 如果兩者相等,則將其打印出來。

演示:

import java.util.Map;
import java.util.HashMap;

public class Example {
    private static final String[] words = {"fine", "rest", "door", "shine"};
    private static final char[] chars = {'t', 'r', 'e', 's'};

    /**
     * Initialise String character counts in a hashmap datastructure.
     * @param word The string word to iterate.
     * @return Hashmap of character -> counts.
     */
    private static Map<Character, Integer> getCharCounts(String word) {
        Map<Character, Integer> wordCounts = new HashMap<>();

        for (int i = 0; i < word.length(); i++) {
            Character character = word.charAt(i);

            // If key doesn't exist, initialise it
            if (!wordCounts.containsKey(character)) {
                wordCounts.put(character, 0);
            }

            // Increment count by 1
            wordCounts.put(character, wordCounts.get(character) + 1);
        }

        return wordCounts;
    }

    public static void main(String[] args) {     
        // Initialise character counts first
        Map<Character, Integer> charCounts = getCharCounts(String.valueOf(chars));

        for (int i = 0; i < words.length; i++) {
            Map<Character, Integer> wordCounts = getCharCounts(words[i]);

            // If Hashmaps are equal, then we have found a match
            if (wordCounts.equals(charCounts)) {
                System.out.println(words[i] + ", at index = " + i);
                break;
            }
        }
    }
}

輸出:

rest, at index = 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM