简体   繁体   English

使用 Java 比较 2 char 数组中的字符

[英]Comparing characters in 2 char Array using Java

I am a beginner in Java and I will like to know if there's a way to compare characters in a char Array with other characters in another char Array in order to see if they have characters that match.我是 Java 的初学者,我想知道是否有办法将 char 数组中的字符与另一个 char 数组中的其他字符进行比较,以查看它们是否有匹配的字符。 Not to see if they contain exactly the same characters in the same sequence as most examples explain.不看它们是否包含与大多数示例解释的相同序列中的完全相同的字符。

For instance:例如:

char [] word1= {'a','c','f','b','e'};

char[] word2= {'a','b','c','d','e','h','j','f','i','m'};

and using maybe if statements to say that word1 contains the characters in word2 so it is correct.并使用 if 语句说 word1 包含 word2 中的字符,所以它是正确的。 Else it is incorrect if word2 is missing at least one character that word1 has.否则,如果 word2 至少缺少 word1 具有的一个字符,则它是不正确的。

I created a little snippet and think it is what you are looking for:我创建了一个小片段,并认为这是您正在寻找的:

public class Main {

    public static void main(String[] args) {
        char[] word1= {'a','c','f','b','e'};
        char[] word2= {'a','b','c','d','e','h','j','f','i','m'};
    
        System.out.println(Contains(word1, word2));
    }

    private static boolean Contains(char[] arr1, char[] arr2) {
        for (int i = 0; i < arr1.length; i++) {
            boolean containsChar = false;
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i] == arr2[j]) containsChar = true;
            }
            if (!containsChar) return false; // arr2 does not contain arr1[i]
        }
        return true;
    }

}

Try this.尝试这个。 It will report if either array contains all the characters of the other.它将报告任何一个数组是否包含另一个数组的所有字符。 I used strings and converted them to arrays to facilitate the coding.我使用字符串并将它们转换为 arrays 以方便编码。

String[][] testData =
                { {"axyzx","xxyzaa"},
                  {"aaxyz","aaax"},
                  {"axa","a"},
                  {"arrs","asrrrs"},
                  {"acfbe","abcdehjfim"}};

for (String[] words : testData) {
    boolean result = contains(words[0].toCharArray(), words[1].toCharArray());
    String output = String.format("%s contains all %s","'"+words[0]+"'","'"+words[1]+"'");
    System.out.printf("%34s - %b%n", output, result);
    output = String.format("%s contains all %s","'"+words[1]+"'","'"+words[0]+"'");
    result = contains(words[1].toCharArray(), words[0].toCharArray());
    System.out.printf("%34s - %b%n%n", output, result);
}

Prints印刷

     'axyzx' contains all 'xxyzaa' - false
     'xxyzaa' contains all 'axyzx' - true

       'aaxyz' contains all 'aaax' - false
       'aaax' contains all 'aaxyz' - false

            'axa' contains all 'a' - true
            'a' contains all 'axa' - false

      'arrs' contains all 'asrrrs' - false
      'asrrrs' contains all 'arrs' - true

 'acfbe' contains all 'abcdehjfim' - false
 'abcdehjfim' contains all 'acfbe' - true



  • Using a map, do a frequency count of the characters in the second array.使用 map,对第二个数组中的字符进行频率计数。
  • Now iterate thru the map using the first array, decrementing the character count when the character is found.现在使用第一个数组遍历 map,找到字符时减少字符数。 When the count reaches 0, assign null to the value.当计数达到 0 时,将null分配给该值。
  • if map is now "empty", the first array contains all characters of second array.如果map现在为“空”,则第一个数组包含第二个数组的所有字符。
// see if first array contains all of second array,
// regardless of order of the characters
public static boolean contains(char[] ch1, char[] ch2) {
//  a smaller array cannot possible contain the same
//  characters as a larger array
    if (ch1.length < ch2.length) {
        return false;
    }
    Map<Character,Integer> map = new HashMap<>();
    // Do a frequency count
    for(char c : ch2) { 
        map.compute(c, (k,v)->v == null ? 1 : v+1);
    }
    // now decrement count for each occurrence
    // of character in first array, setting value to
    // null when count reaches 0.
    for(char c : ch1) {
        map.computeIfPresent(c, (k,v)-> v <= 1 ? null : v-1);
    }
    return map.isEmpty();
}

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

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