2

私はプログラミングの最初のクラスにいて、2つのリストを組み合わせて1つのリストを作成し、新しいリストを番号順に並べようとしています。私が問題を抱えているのは、コードをループさせ、手順を繰り返して元のループ全体を実行し、元のリストのすべての数値を組み合わせた最終的なリストを完成させることです。ループのガイダンスをいただければ幸いです。ありがとうございました。

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
4

3 に答える 3

1

どうですか:

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

または、何か不足していますか?

于 2012-10-06T21:45:08.523 に答える
0

2 つのファイル / リスト / ストリームをマージするには、次のようなループが必要です

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

では、終了したことをどのように知ることができますか?

各リストの次の項目の最小値を取得するにはどうすればよいでしょうか?

上で書いたコードは疑似コードと呼ばれます。これは、アルゴリズムのステップを説明する方法です。選択した言語 (この場合は Java) で実装できる疑似コードができるまで、各ステップを拡張し続けます。

それが役立つことを願っています...

于 2012-10-06T21:53:44.413 に答える
0

あなたの問題は、2つのリストのサイズが不均一である可能性があるためだと思います。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
}

また、注意してください: merge2 つのソートされたリスト要素を実行するより良い方法があります。このアプローチは、ほとんど知られていないカスタムクラスに照らして推奨されます。List

于 2012-10-06T22:04:09.700 に答える