我正在开发一个程序,我们比较两个字符串,并将一个字符串中不存在的字符添加到另一个字符串,反之亦然。 例如:String 1- aabccd 字符串2- acccdd 输出应该是 - 要添加到字符串1- cd的字符 要添加到String 2- ab的字符 当角色不重复时,我能够实现它, 例如:String 1-芒果 弦乐2盎司 输出是 - 要添加到字符串1- l的字符 要添加到字符串2- m的字符 但是当角色重复时却无法得到它。
这是我写的代码 -
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
String S1, S2;
System.out.println("Enter String 1: ");
S1 = user_input.next();
System.out.println("Enter String 2: ");
S2 = user_input.next();
user_input.close();
char[] S1Array = S1.toLowerCase().toCharArray();
char[] S2Array = S2.toLowerCase().toCharArray();
charAddition(S1Array, S2Array);
charAdditionReverse(S2Array,S1Array);
}
private static void charAddition(char[] n, char[] S1Array) {
for (char n1 : n) {
if (!isPresent(n1, S1Array)) {
System.out.println("character to be added to S2Array
is: " + n1);
}
}
}
private static void charAdditionReverse(char[] n, char[] S1Array) {
for (char n1 : n) {
if (!isPresent(n1, S1Array)) {
System.out.println("character to be added to S1Array
is: " + n1);
}
}
}
private static boolean isPresent(char n, char[] S1Array) {
for (char i : S1Array) {
if (n == i) {
return true;
}
}
return false;
}
}
答案 0 :(得分:0)
以下是您的解决方案:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
String S1, S2;
System.out.println("Enter String 1: ");
S1 = user_input.next();
System.out.println("Enter String 2: ");
S2 = user_input.next();
user_input.close();
char[] S1Array = S1.toLowerCase().toCharArray();
char[] S2Array = S2.toLowerCase().toCharArray();
charAddition(S1Array, S2Array,false);
charAddition(S2Array,S1Array,true);
}
private static void charAddition(char[] n, char[] S1Array,boolean isreverse)
{
List<char[]> asList = Arrays.asList(S1Array); // because this DOES compile.
List<Character> wordlist = new ArrayList<Character>();
for (char c : S1Array) {
wordlist.add(c);
}
for (char n1 : n)
{
if (!isPresent(n1, wordlist))
{
if(!isreverse)
{
System.out.println("character to be added to S2Arrayis: " + n1);
}else
{
System.out.println("character to be added to S1Arrayis: " + n1);
}
}
}
}
private static boolean isPresent(char n, List<Character> S1Array)
{
boolean flag ;
for (Iterator<Character> it = S1Array.iterator(); it.hasNext();)
{
char i = it.next();
if (n == i)
{
it.remove();
return true;
}
}
return false;
}
}
要解决上述问题,您必须检查特定字符重复的次数。这就是我从数组传递ArrayList到函数创建新数组列表并删除已经匹配的元素的原因。我已经将2个功能改为一个,因为我认为这是编写代码的更好方法。
答案 1 :(得分:0)
完全不同的方法:
void disjoint(String s1, String s2) {
// Convert two strings to character lists
List<Character> chars1 = s1.chars()
.mapToObj(t -> (char) t)
.collect(Collectors.toList());
List<Character> chars2 = s2.chars()
.mapToObj(t -> (char) t)
.collect(Collectors.toList());
// Make a list of elements we are going to remove from chars1
List<Character> removedElements = new ArrayList<>();
chars1.forEach(t -> {
// Try to remove the current character from chars2, and if it has
// successfully been removed, add it to our removedElements list
if (chars2.remove(t)) {
removedElements.add(t);
}
});
// At last remove each removed element from chars2 also from chars1
removedElements.forEach(chars1::remove);
System.out.println(chars1);
System.out.println(chars2);
}