使用 Java 比较 2 个字符数组中的字符
Comparing characters in 2 char Array using Java
我是 Java 的初学者,我想知道是否有一种方法可以将一个 char 数组中的字符与另一个 char 数组中的其他字符进行比较,以查看它们是否具有匹配的字符。不要看它们是否包含与大多数示例解释的相同序列完全相同的字符。
例如:
char [] word1= {'a','c','f','b','e'};
char[] word2= {'a','b','c','d','e','h','j','f','i','m'};
并使用 maybe if 语句表明 word1 包含 word2 中的字符,因此它是正确的。否则,如果 word2 至少缺少 word1 具有的一个字符,则不正确。
我创建了一个小片段,我认为这就是您要找的:
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;
}
}
试试这个。它将报告任一数组是否包含另一个数组的所有字符。我使用字符串并将它们转换为数组以方便编码。
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);
}
版画
'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
- 使用地图,对第二个数组中的字符进行频率计数。
- 现在使用第一个数组遍历地图,在找到字符时减少字符计数。当计数达到 0 时,将
null
赋值给值。
- 如果
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();
}
我是 Java 的初学者,我想知道是否有一种方法可以将一个 char 数组中的字符与另一个 char 数组中的其他字符进行比较,以查看它们是否具有匹配的字符。不要看它们是否包含与大多数示例解释的相同序列完全相同的字符。
例如:
char [] word1= {'a','c','f','b','e'};
char[] word2= {'a','b','c','d','e','h','j','f','i','m'};
并使用 maybe if 语句表明 word1 包含 word2 中的字符,因此它是正确的。否则,如果 word2 至少缺少 word1 具有的一个字符,则不正确。
我创建了一个小片段,我认为这就是您要找的:
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;
}
}
试试这个。它将报告任一数组是否包含另一个数组的所有字符。我使用字符串并将它们转换为数组以方便编码。
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);
}
版画
'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
- 使用地图,对第二个数组中的字符进行频率计数。
- 现在使用第一个数组遍历地图,在找到字符时减少字符计数。当计数达到 0 时,将
null
赋值给值。 - 如果
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();
}