Код решения ниже, валидатор принял. Вопрос такой - как определить конечный размер списка lines ?? Пришлось вставить костыль в виде копии списка, дать ему размер в виде суммы строк в обоих файлах, а потом, заполнив его, скопировать в нужный по условию, что строка не равна "". Кто подскажет решение без этого костыля?
package com.javarush.task.task19.task1916;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static List<LineItem> copy = new ArrayList<LineItem>();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader file1 = new BufferedReader(new FileReader(br.readLine()));
BufferedReader file2 = new BufferedReader(new FileReader(br.readLine()));
/*BufferedReader file1 = new BufferedReader(new FileReader("c:/test/or.txt"));
BufferedReader file2 = new BufferedReader(new FileReader("c:/test/end.txt"));*/
ArrayList<String> firstFile = new ArrayList<>();
ArrayList<String> secondFile = new ArrayList<>();
String line = "";
while ((line = file1.readLine()) != null)
firstFile.add(line);
while ((line = file2.readLine()) != null)
secondFile.add(line);
int counter = firstFile.size() + secondFile.size();
for (int i = firstFile.size(); i < counter; i++)
firstFile.add("");
for (int i = secondFile.size(); i < counter; i++)
secondFile.add("");
int i = 0, j = 0, c = 0;
while (c < counter) {
if (firstFile.get(i).equals(secondFile.get(j))) {
copy.add(new LineItem(Type.SAME, firstFile.get(i)));
i++;
j++;
} else if (firstFile.get(i + 1).equals(secondFile.get(j))) {
copy.add(new LineItem(Type.REMOVED, firstFile.get(i)));
i++;
} else if (firstFile.get(i).equals(secondFile.get(j + 1))) {
copy.add(new LineItem(Type.ADDED, secondFile.get(j)));
j++;
}
c++;
}
for (LineItem item : copy)
if (!item.line.equals(""))
lines.add(item);
br.close();
file1.close();
file2.close();
}
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;
}
}
}