【问题标题】:Looping my list so that runs through and combines the whole list循环我的列表,以便贯穿并组合整个列表
【发布时间】:2012-09-27 15:44:51
【问题描述】:

我正在上编程的初级课程,并尝试将 2 个列表组合成一个列表,并将新列表按数字顺序排列。我遇到问题的部分是,允许代码循环,重复这些步骤,以便它运行整个原始循环以完成最终列表,该列表是原始列表中所有数字的组合。任何有关循环的指导将不胜感激。谢谢你。

import inClass.list.EmptyListException;
import inClass.list.List;

public class InitialLists {

    public static void main(String[] args) {

    List<Integer> intObject1 = new List<Integer>();{

        intObject1.insertAtFront(25);

        intObject1.insertAtFront(19);

        intObject1.insertAtFront(3);

        intObject1.print();}

    List<Integer> intObject2 = new List<Integer>();{

        intObject2.insertAtFront(120);

        intObject2.insertAtFront(1);

        intObject2.print();}

    List<Integer> combinedList = new List<Integer>();

    int object1 = intObject1.removeFromBack();
    int object2 = intObject2.removeFromBack();

        while(intObject1.removeFromBack() != null && intObject2.removeFromBack() != null){

    try {


        {
            if (intObject1.removeFromBack() > intObject2.removeFromBack()) {
                combinedList.insertAtFront(object2);
                intObject1.insertAtBack(object1);
            }           
            else if (intObject2.removeFromBack() < intObject1.removeFromBack()) {
                combinedList.insertAtFront(object1);
                intObject2.insertAtBack(object2);
            }   
            else if (intObject1.removeFromBack() == intObject2.removeFromBack()) {
                combinedList.insertAtFront(object1);
            }
        }   
            combinedList.print();

            object1 = intObject1.removeFromBack();
            object2 = intObject2.removeFromBack();

        } // end try

        catch (EmptyListException emptyListException) {
            emptyListException.printStackTrace();
        } // end catch
        } //end while
    } // end main

}// end class

【问题讨论】:

  • 我认为这甚至不会编译 - 你不能拥有 new List() 因为 List 是一个接口。您还有一些不必要的 {} 块。您是否考虑过以无序的方式组合列表,然后对最终列表进行排序?
  • @DNA 它使用自定义的List,注意.insertAtFront().removeFromBack() 方法。
  • @kassie 您可以查看mergesort 的想法。
  • 啊,是的,发布得太早了。虽然重用标准 List 类名有点令人困惑 - 这对于 IMO 的教学任务来说不是很好的做法。
  • 鉴于您使用的是非标准列表,您的List 类实际上提供了哪些方法?在不知道的情况下很难回答......

标签: java list loops while-loop


【解决方案1】:

怎么样:

List<Integer> combinedList = new ArrayList<Integer>();
combinedList.addAll(intObject1);
combinedList.addAll(intObject2);
Collections.sort(combinedList);

还是我错过了什么?

【讨论】:

  • 这看起来像是一个家庭作业,所以我怀疑 OP 是否会被允许这样做。
  • 这正是我讨厌家庭作业的地方:它们教你不要使用高质量和经过良好测试的代码,并导致长期的“不是这里发明的”综合症。
  • +1,因为我正要提出完全相同的建议。问题需要澄清此分配允许哪些操作 - 例如,我们不知道自定义 List 类型是否扩展了 java.util.List...
  • 谢谢大家的想法。是的。功课是肯定的。我已经投入了很多很多小时。所以我非常感谢你的想法和帮助。他们让我们使用的 java.util.List 在这里:
  • 其实在哪里可以添加List的代码呢?这告诉我太多的字符。对不起!显然是新手
【解决方案2】:

要合并两个文件/列表/流,您需要一个看起来有点像这样的循环

WHILE NOT FINISHED
    GET SMALLEST VALUE FROM INPUTS
    APPEND SMALLEST VALUE TO OUTPUT

那么你怎么知道你已经完成了?

你将如何获得每个列表中下一个项目中最小的?

我上面写的代码叫做伪代码;它是描述算法步骤的一种方式。继续扩展每个步骤,直到您拥有可以用您选择的语言(在本例中为 Java)实现的伪代码。

希望对您有所帮助...

【讨论】:

    【解决方案3】:

    我猜你的问题是因为两个列表的大小可能不均匀。尝试将while 条件如下:

    Integer object1 = intObject1.removeFromBack();
    Integer object2 = intObject2.removeFromBack();
    while(object1 != null || object2!= null){
       if(object1 ==null){
           //safe to assume object2 is not null as both not null together (that is the termination condition)
           combinedList.insertAtFront(object2);
       }else if(object2 ==null){
           //safe to assume object1 is not null as both not null together (that is the termination condition)
           combinedList.insertAtFront(object1);
       }else{
           //put you normal condition of handling object1 and object2 being not null
            if (object1.intValue() > object2.removeFromBack()) {
                combinedList.insertAtFront(object2);
                intObject1.insertAtBack(object1);
            }           
            else if (object2.intValue() < object1.intValue()) {
                combinedList.insertAtFront(object1);
                intObject2.insertAtBack(object2);
            }   
            else if (object1.intValue() == object2.intValue()) {
                combinedList.insertAtFront(object1);
            }
       }
       object1 = null;
       object2 = null;
       try{
            object1 = intObject1.removeFromBack();
       }catch (EmptyListException emptyListException) {
           //do nothing
        } // end catch
       try{
            object2 = intObject2.removeFromBack();
       }catch (EmptyListException emptyListException) {
           //do nothing
        } // end catch
    }
    

    另外请注意:有更好的方法来处理两个排序列表元素的merge鉴于您鲜为人知的自定义 List 类,建议使用此方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2021-12-07
      • 1970-01-01
      • 2019-04-30
      相关资源
      最近更新 更多