【问题标题】:Can't merge two arraylists无法合并两个数组列表
【发布时间】:2012-03-21 01:11:52
【问题描述】:

我正在尝试自己学习 Java,而书中的一个问题是交给我的。这是关于在对它们进行 MergeSort 样式排序后合并两个 ArrayList 。如果没有重大灾难,我无法将它们重新组合在一起。我真的很想知道,这样我才能继续前进,这让我发疯了。

 public <E extends Comparable<?super E>> void merge(List<E> front, List<E> back, boolean last, List<E> result){
  int i = 0;
  int j = 1;
    while (!front.isEmpty() && !back.isEmpty()) {
      if (front.get(0).compareTo(back.get(0)) <= 0) {
            result.add(result.size() -1, front.remove(0));
            System.out.println("works" + result);
          } else {
            result.add(result.size() -1, back.remove(0));
            System.out.println("no work" + result);
          }
    }
     while (!front.isEmpty()) {
        result.add(result.size() -1,front.remove(0));
    }
    while (!back.isEmpty()) {
        result.add(result.size() - 1, back.remove(0));
    }
System.out.println();
} } 

布尔值是将它们排序为:true==升序,false==降序。我可以为此担心。我们将不胜感激任何类型的帮助。

【问题讨论】:

  • "...书中的一个问题是把我的问题交给我"。这是什么语言?它看起来不像英语。 (请记住,有 21 岁以上的人在阅读这篇文章,以及第一语言不是英语的人。)
  • “大灾大难”是什么意思?具体来说,出了什么问题?它会抛出异常吗?它不编译吗?它会返回意外的结果吗?

标签: java


【解决方案1】:

我认为您所要做的就是进行以下更改

result.add(front.remove(0));

else 子句也有同样的变化。

要在结果列表的末尾添加一个元素,您不应指定索引。

【讨论】:

    【解决方案2】:

    如果您的数组列表已排序,请遍历它们并仅在一端取最小元素(假设您想要升序)。这样做直到其中一个列表为空。然后只需将剩余列表中的条目附加到合并列表中即可。

    【讨论】:

    【解决方案3】:

    嗯,我用来实现合并的一种安全方法如下所示。

    public <E extends Comparable<? super E>> void merge(List<E> front,
                List<E> back, boolean last, List<E> result) {
            int i = 0;
            int j = 0;
            Collections.sort(front);
            Collections.sort(back);
            while (!(i == front.size() && j == back.size())) {
                E e;
                if (i == front.size()) {
                    // If we have exhausted the 'front' then copy whatever is in
                    // 'back'
                    e = back.get(j);
                    // Move back index to next
                    j++;
                } else if (j == back.size()) {
                    // If we have exhausted the 'back' then copy whatever is in
                    // 'front'
                    e = front.get(i);
                    // Move front index to next
                    i++;
                } else {
                    if ((front.get(i).compareTo(back.get(j)) <= 0)) {
                        // front item will be added, increment front index for next
                        e = front.get(i);
                        i++;
                    } else {
                        // back item will be added, increment back index for next
                        e = back.get(j);
                        j++;
                    }
                }
                result.add(e);
            }
            if(last){
                Collections.reverse(result);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 2020-02-19
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      相关资源
      最近更新 更多