繁体   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