Все работает, но он считает, что removed не везде есть.
package com.javarush.task.task19.task1916;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file1 = reader.readLine();
String file2 = reader.readLine();
reader.close();
BufferedReader reader1 = new BufferedReader(new FileReader(file1));
BufferedReader reader2 = new BufferedReader(new FileReader(file2));
List<String> list1 = new ArrayList<>();
while (reader1.ready()) {
list1.add(reader1.readLine());
}
reader1.close();
List<String> list2 = new ArrayList<>();
while (reader2.ready()) {
list2.add(reader2.readLine());
}
reader2.close();
int difference = 0;
int temp;
int lastMatch_i = 0;
int lastMatch_j = 0;
for (int j = 0; j < list2.size(); j++) {
for (int i = 0; i < list1.size(); i++) {
if (list2.get(j).equals(list1.get(i))) {
lines.add(new LineItem(Type.SAME, list2.get(j)));
lastMatch_j = j;
lastMatch_i = i;
temp = j - i;
if (j - 1 >= 0 && i - 1 >= 0) {
if (!list2.get(j - 1).equals(list1.get(i - 1))) {
if (temp - difference > 0) {
lines.add(lines.size() - 1, new LineItem(Type.ADDED, list2.get(j - 1)));
}
else if (temp - difference < 0) {
lines.add(lines.size() - 1, new LineItem(Type.REMOVED, list1.get(i - 1)));
}
}
}
difference = temp;
}
}
}
if (lastMatch_i != (list1.size() - 1)) {
lines.add(new LineItem(Type.REMOVED, list1.get(list1.size() - 1)));
}
else if (lastMatch_j != (list2.size() - 1)) {
lines.add(new LineItem(Type.ADDED, list2.get(list2.size() - 1)));
}
}
public static enum Type {
ADDED, //добавлена новая строка
REMOVED, //удалена строка
SAME //без изменений
}
public static class LineItem {
public Type type;
public String line;
public LineItem(Type type, String line) {
this.type = type;
this.line = line;
}
}
}