java - 如何迭代列表并创建自定义非顺序输出

标签 java linked-list

我必须比较两个单独文件的行并以特定方式输出结果(保存到单独的列表中)。 File1 是原始文本。 File2 具有一些相同的行或新行。这意味着 File2 中缺少 File1 中存在的某些行。

例如:
文件1:
一个
B
C
D
E

文件2:
B
C
D
F
G

自定义排序前组合列表的输出:
A - 已删除
B - 相同
C - 相同
D - 相同
E - 已删除
F-新
G - 新

我需要做的是按以下方式输出列表:
A - 已删除
B - 相同
E - 已删除
C - 相同
F-新
D - 相同
G - 新

基本上,相同的行需要在每个第二个索引上显示(或最初保存?)。

到目前为止,我已经成功地比较了文件(通过将每个文件中的行添加到两个单独的链接列表并比较它们),并创建了一个组合列表,并将行状态(即相同、已删除、新)添加到每行。
然而,我只是无法理解这种自定义排序的算法,因为我是 Java 新手。 我是否需要创建一个自定义迭代器并将其传递给我的组合列表?或者可以使用默认的 listIterator 来实现吗?

到目前为止的代码:

public class Solution {
    public static List<LineItem> lines = new ArrayList<>();

    public static void main(String[] args) {

        try (BufferedReader fileOne = new BufferedReader(new FileReader("file1.txt"));
             BufferedReader fileTwo = new BufferedReader(new FileReader("file2.txt"))) {

            List<String> listOne = new LinkedList<>();
            List<String> listTwo = new LinkedList<>();
            List<LineItem> listOfSame = new LinkedList<>();
            List<LineItem> result = new ArrayList<>();

            String line;

            while ((line = fileOne.readLine()) != null) {
                listOne.add(line);
            }

            while ((line = fileTwo.readLine()) != null) {
                listTwo.add(line);
            }

            for (String s : listOne) {
                if (listTwo.contains(s)) {
                    listOfSame.add(new LineItem(Type.SAME, s));
                } else if (!listTwo.contains(s)) {
                    lines.add(new LineItem(Type.REMOVED, s));
                }
            }

            for (String s : listTwo) {
                if (!listOne.contains(s)) {
                    lines.add(new LineItem(Type.ADDED, s));
                }
            }


            Iterator<LineItem> itSame = listOfSame.iterator();

            for (int i = 0; i < lines.size(); i++) {
                result.add(lines.get(i));

                if (i % 2 == 0 && itSame.hasNext()) {
                   result.add(itSame.next());
                }
            }

            while (itSame.hasNext()) {
                result.add(itSame.next());
            }

            for (LineItem item : result) {
              System.out.println(item.type.toString() + " " + item.line);                
            }


        } catch (IOException e) { e.printStackTrace(); }

    }

    public static enum Type {
        ADDED,        // New line added
        REMOVED,      // Line deleted
        SAME          // No change
    }

    public static class LineItem {
        public Type type;
        public String line;

        public LineItem(Type type, String line) {
            this.type = type;
            this.line = line;
        }
    }
}

最佳答案

我想您可以将 SAME 项移动到一个单独的列表中,然后遍历两个列表并每隔一次迭代输出一个 SAME 行(执行伪 merge sort )。像这样的东西:

List<LineItem> sameOnes = new ArrayList();
Iterator<LineItem> it = lines.iterator();
while (it.hasNext()) {
  LineItem item = it.next();
  if (item.type == Type.SAME) {
    sameOnes.add(item);
    it.remove();
  }
}

然后:

List<LineItem> result = new ArrayList<>();
Iterator<LineItem> sameIt = sameOnes.iterator();
for (int i = 0; i < lines.size(); i++) {
  result.add(lines.get(i));
  // Ensure there still are same lines to output
  if (sameIt.hasNext()) {
    result.add(sameIt.next());
  }
}
// Fill in the rest in case there were more same than different lines
while (sameIt.hasNext()) {
  result.add(sameIt.next());
}

关于java - 如何迭代列表并创建自定义非顺序输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60465947/

相关文章:

java - 从其他类调用非静态方法

java - Java 中将两个列表合并到一张 map 的最佳方法?

java - 从文件读入构造函数时出现空行

JAVA:如何在二进制文件中正确存储对象?不覆盖

java - 将 inOrderTraversal 方法的内容放入文件时遇到问题

c - 我正在尝试编写一个函数,从给定的链表中删除所有奇数元素并返回一个地址

java - 获取以 Apache 速度显示在浏览器中的请求 URI

c++ - 模数为哈希表中的地址创建错误的 int?

c++ - 链表 - 指针

c++ - 重载不适用于 <<