我的程序的目标是比较两个文本文件,并打印其中一个文本文件的罕见子序列。当我定义两个字符串数组,例如
String [] s1 = {"b","a","b","a","b"};
String [] s2 = {"b","a","b","b","a"};
进行比较,就可以了。 (它打印:[b a b a b , b a a b , b b a b , a b a b , a a b ]
)但是它不适用于文本文件。该程序怎么了?我没有得到任何输出,程序也没有终止,这可能是由于无限循环造成的。
public class hess {
static ArrayList<String> arr1 = new ArrayList<String>();
static ArrayList<String> arr2 = new ArrayList<String>();
public void printAllSubSequences(String[] arrInput) {
int[] temp = new int[arrInput.length];
int index = 0;
solve(arrInput, index, temp);
}
private void solve(String[] arrInput, int index, int[] temp) {
if (index == arrInput.length) {
print(arrInput, temp);
return;
}
// set the current index bit and solve it recursively
temp[index] = 1;
solve(arrInput, index + 1, temp);
// unset the current index bit and solve it recursively
temp[index] = 0;
solve(arrInput, index + 1, temp);
}
private void print(String[] arrInput, int[] temp) {
String result = "";
for (int i = 0; i < temp.length; i++) {
if (temp[i] == 1)
result += arrInput[i] + " ";
}
arr1.add(result);
}
public void printAllSubSequences1(String[] arrInput) {
int[] temp = new int[arrInput.length];
int index = 0;
solve1(arrInput, index, temp);
}
private void solve1(String[] arrInput, int index, int[] temp) {
if (index == arrInput.length) {
print1(arrInput, temp);
return;
}
// set the current index bit and solve it recursively
temp[index] = 1;
solve1(arrInput, index + 1, temp);
// unset the current index bit and solve it recursively
temp[index] = 0;
solve1(arrInput, index + 1, temp);
}
private void print1(String[] arrInput, int[] temp) {
String result = "";
for (int i = 0; i < temp.length; i++) {
if (temp[i] == 1)
result += arrInput[i] + " ";
}
arr2.add(result);
}
public static void main(String[] args) throws IOException {
String[] s1 = read("C:\\Users\\fener\\Desktop\\producerconsumer\\1575229506551OutBug.txt");
String[] s2 = read("C:\\Users\\ahmet\\Desktop\\bitirme\\producerconsumer\\1575371318152Out.txt");
new hess().printAllSubSequences(s1);
new hess().printAllSubSequences1(s2);
System.out.println(arr1);
System.out.println(arr2);
arr1.removeAll(arr2);
System.out.println(arr1);
}
private static String[] read(String location) throws IOException {
BufferedReader reader1 = new BufferedReader(new FileReader(location));
String line;
ArrayList<String> lines = new ArrayList<String>();
while ((line = reader1.readLine()) != null) {
lines.add(line);
}
String[] result = new String[lines.size()];
for(int i=0; i<lines.size(); i++) {
result[i] = lines.get(i);
}
return result;
}
}
'''