我希望逐个字符地比较多个字符串,并希望输出一个字符串,其中包含大多数字符串中存在的字符。 例如,我使用了三个字符串: 输入:S1 =其中,S2 = wHere,S3 = whera 输出:S =其中
我可以使用代码执行3个字符串:
public class stringc {
static String S1="where";
static String S2="wHere";
static String S3="whera";
static StringBuilder S=new StringBuilder();
public static void main(String[] args)
{
for (int i=0;i<5;i++)
{
if(S1.charAt(i)==S2.charAt(i)||S1.charAt(i)==S3.charAt(i))
{
S.append(S1.charAt(i));
}
}
}
}
任何人都可以帮助我如何使用它超过10个字符串。
答案 0 :(得分:0)
使用数组/字符串列表而不是int i=0;i<5;i++
使用int i=0;i<array.length;i++
//使用list.size()
列表
答案 1 :(得分:0)
我是怎么做到的:
toCharArray()
以下是使用您的示例以及更多字符串的代码块:
public static void main(final String[] args) {
final StringBuilder stringBuilder = new StringBuilder();
//Points 1 and 2
final char[][] characters = new char[][]{
"where".toCharArray(),
"wHere".toCharArray(),
"where".toCharArray(),
"wperg".toCharArray(),
"where".toCharArray(),
"w6ere".toCharArray(),
"where".toCharArray(),
"where".toCharArray(),
"wHere".toCharArray(),
"w4eeg".toCharArray(),
"where".toCharArray(),
"wHare".toCharArray(),
"where".toCharArray(),
"where".toCharArray(),
"weede".toCharArray(),
"whare".toCharArray(),
"wHect".toCharArray(),
"where".toCharArray(),
"wHere".toCharArray(),
"whara".toCharArray()
};
//Point 3
for (int i=0; i < 5 ; i++) {
//Point 4
final Map<String, Integer> occurrences = new HashMap<String, Integer>();
for(int j = 0; j < characters.length; j++ ) {
final String character = ""+characters[j][i];
if( occurrences.containsKey(character) ) {
Integer currentTotal = occurrences.get(character);
occurrences.put(character, ++currentTotal);
continue;
}
occurrences.put(character, 1);
}
//Point 5
int mostOccurrences = 0;
String characterWithMostOccurrences = "";
for (final String character : occurrences.keySet()) {
if( occurrences.get(character) > mostOccurrences ) {
mostOccurrences = occurrences.get(character);
characterWithMostOccurrences = character;
}
}
stringBuilder.append(characterWithMostOccurrences);
}
//Point 6
System.out.println(stringBuilder.toString());
}
答案 2 :(得分:0)
这是我敲了一个。它会比较输入字符串中的字符,以检查它们在同一位置的每个字符串中是否相同。 如果位置中的字符在所有输入字符串中匹配,则相同的字符将位于该位置的结果字符串中。
public class MultipleStringComparator {
public static void main(String[] args) {
String[] input = new String[] {"abcdeFgh","abcdefgh","abcdefgh","abcdefgh","abcdefgh"};
MultipleStringComparator comp = new MultipleStringComparator();
System.out.println(comp.compare(input, '.'));
}
/**
* Compare strings character by character
* @param strings
* @param substitute - character to insert in result where strings do not match
* @return result
*/
public String compare(String[] strings, char substitute) {
//How long is the longest string?
int longest = 0;
for (String string : strings) {
if (string.length()>longest) longest = string.length();
}
//Initialise result
StringBuffer result = new StringBuffer();
//compare strings character by character
//If the corresponding characters in all input strings match, append to result
for (int position=0; position<longest; position++) {
char character = allCharactersMatch(strings, position);
if (character!=0) {
result.append(character);
} else {
result.append(substitute);
}
}
return result.toString();
}
/**
* Compares the character at the specified position in all input strings
* @param strings
* @param position
* @return character found is same in all strings, otherwise 0;
*/
private char allCharactersMatch(String[] strings, int position) {
char found = 0;
for (String string : strings) {
if (string.length()<=position) return 0;
if (string.charAt(position)!=found && found!=0) return 0;
found = string.charAt(position);
}
return found;
}
}