简体   繁体   中英

Looping my list so that runs through and combines the whole list

I am in a beginning class for programming and try to combine 2 lists to make one list, putting the new list in numerical order. The part I am having trouble with is, allowing the code to loop, repeating the steps so that it runs through the total original loops to complete the final list which is a combination of all the numbers from the original lists. Any guidance for the loop would be appreciated. Thank you.

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

What about:

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

Or am I missing something?

To merge two files / lists / streams you need a loop that looks a bit like this

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

So how will you know that you are finished?

How will you get the smallest of the next item in each list?

The code I have written above is called pseudocode; it is a way of describing the steps of an algorithm. Keep expanding each step until you have pseudocode that you can implement in your chosen language, in this case Java.

Hope that helps ...

I guess your problem is because of possible uneven size of two lists. Try putting while condition as below:

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
}

Also please note: There are better way of doing the merge of two sorted list elements. This approach is advised in light of your little known custom List class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM