Solved

Merge two lists java

Posted on 2011-03-24
7
849 Views
Last Modified: 2013-11-23
Add a mergeLists method to the class OrderedLinkedList.  The mergeLists method shall accept two lists as inputs and creates a new list with all elements merged and in proper order.  Suppose list1 points to the list with the elements 2, 6, and 7 and list2 points to the list with the elements 3, 5, and 8.  The method call newlist.mergeLists(list1,list2);  should produce a list with the elements 2, 3, 5, 6, 7, and 8.  YOU MAY NOT USE THE INSERT METHOD PROVIDED IN THE ORDEREDLINKEDLIST CLASS.  Be sure to write the definition of the method mergeLists and thoroughly test your program.

I have created the method but I havent been able to get the merged list to print out in the TestProgOrderedLinkedList.java file. Please help
LinkedListADT.java
LinkedListClass.java
OrderedLinkedList.java
TestProgOrderedLinkedList.java
0
Comment
Question by:wjenlow
7 Comments
 
LVL 4

Expert Comment

by:msk_apk
ID: 35214949
public List mergeLists(List first,List second)
      {
                List resultList = new ArrayList();
              if(first == null || first.size() ==0)
          {
             if(second == null || second.size() ==0)
            {
                  return resultList;
            }
            else
            {
                  return second;
            }
          }
          else if(second == null || second.size() ==0 )
          {
                return first;
          }

          Iterator firstIterator = first.firstIterator();
            Iterator secondIterator = second.secondIterator();
                      
          Comparable firstObject = (Comparable)firstIterator.next();
              Comparable secondObject = (Comparable) secondIterator.next();                

                 while(firstIterator.hasNext() && secondIterator().hasNext())
          {
            int compResult = firstObject.compareTo(secondObject);
            if(compResult ==0)
            {
                  resultList.add(firstObject);
                  resultList.add(secondObject);
                  if(firstIterator.hasNext())
                  {
                        firstObject = (Comparable)first.next();
                  }
                  if(secondIterator.hasNext())
                  {
                        secondObject = (Comparable)second.next();
                  }
            }
            else if(compResult < 0)
            {
                  resultList.add(firstObject);
                  if(firstIterator.hasNext())
                  {
                        firstObject = (Comparable)first.next();
                  }
            }
            else
            {
                  resultList.add(secondObject);
                  if(secondIterator.hasNext())
                  {
                        secondObject = (Comparable)second.next();
                  }
            }

          }
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35215012
Sounds like homework...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35215192
We can help with homework just not do the whole thing
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:wjenlow
ID: 35215451
I understand that... That's why I wrote the method myself but I couldn't get it to run right. I'm trying to learn this stuff just need help figuring it out :)
0
 

Author Comment

by:wjenlow
ID: 35216713
Still having trouble.. getting errors with the new code.
public numList mergeLists(numList list1,numList list2){
      
         numList resultnumList = new ArrayList();
         if(list1 == null || list1.size() ==0)
         {
            if(list2 == null || list2.size() ==0)
            {
               return resultnumList;
            }
            else
            {
               return list2;
            }
         }
         else if(list2 == null || list2.size() ==0 )
         {
            return list1;
         }
      
         Iterator firstIterator = list1.firstIterator();
         Iterator secondIterator = list2.secondIterator();
                      
         Comparable firstObject = (Comparable)firstIterator.next();
         Comparable secondObject = (Comparable) secondIterator.next();                
      
         while(firstIterator.hasNext() && secondIterator().hasNext())
         {
            int compResult = firstObject.compareTo(secondObject);
            if(compResult ==0)
            {
               resultnumList.add(firstObject);
               resultnumList.add(secondObject);
               if(firstIterator.hasNext())
               {
                  firstObject = (Comparable)list1.next();
               }
               if(secondIterator.hasNext())
               {
                  secondObject = (Comparable)list2.next();
               }
            }
            else if(compResult < 0)
            {
               resultnumList.add(firstObject);
               if(firstIterator.hasNext())
               {
                  firstObject = (Comparable)list1.next();
               }
            }
            else
            {
               resultList.add(secondObject);
               if(secondIterator.hasNext())
               {
                  secondObject = (Comparable)list2.next();
               }
            }
         
         }
      }

Open in new window

public numList mergeLists(numList list1,numList list2);
   }


//Placed in LinkedListADT and got the same error; numList - cannot find symbol

Open in new window

public numList mergeLists(numList list1,numList list2);
   }
//This I placed in the LinkedListClass.java file and get errors stating this: 


 ----jGRASP exec: javac -g TestProgOrderedLinkedList.java

OrderedLinkedList.java:134: cannot find symbol
symbol  : class numList
location: class OrderedLinkedList<T>
      public numList mergeLists(numList list1,numList list2){
                                ^
OrderedLinkedList.java:134: cannot find symbol
symbol  : class numList
location: class OrderedLinkedList<T>
      public numList mergeLists(numList list1,numList list2){
                                              ^
OrderedLinkedList.java:134: cannot find symbol
symbol  : class numList
location: class OrderedLinkedList<T>
      public numList mergeLists(numList list1,numList list2){
             ^
LinkedListClass.java:339: cannot find symbol
symbol  : class numList
location: class LinkedListClass<T>
      public numList mergeLists(numList list1,numList list2);
                                ^
LinkedListClass.java:339: cannot find symbol
symbol  : class numList
location: class LinkedListClass<T>
      public numList mergeLists(numList list1,numList list2);
                                              ^
LinkedListClass.java:339: cannot find symbol
symbol  : class numList
location: class LinkedListClass<T>
      public numList mergeLists(numList list1,numList list2);
             ^
LinkedListADT.java:67: cannot find symbol
symbol  : class numList
location: interface LinkedListADT<T>
      public numList mergeLists(numList list1,numList list2);
                                ^
LinkedListADT.java:67: cannot find symbol
symbol  : class numList
location: interface LinkedListADT<T>
      public numList mergeLists(numList list1,numList list2);
                                              ^
LinkedListADT.java:67: cannot find symbol
symbol  : class numList
location: interface LinkedListADT<T>
      public numList mergeLists(numList list1,numList list2);
             ^
LinkedListClass.java:339: missing method body, or declare abstract
      public numList mergeLists(numList list1,numList list2);
                     ^
OrderedLinkedList.java:136: cannot find symbol
symbol  : class numList
location: class OrderedLinkedList<T>
         numList resultnumList = new ArrayList();
         ^
OrderedLinkedList.java:136: cannot find symbol
symbol  : class ArrayList
location: class OrderedLinkedList<T>
         numList resultnumList = new ArrayList();
                                     ^
OrderedLinkedList.java:153: cannot find symbol
symbol  : class Iterator
location: class OrderedLinkedList<T>
         Iterator firstIterator = list1.firstIterator();
         ^
OrderedLinkedList.java:154: cannot find symbol
symbol  : class Iterator
location: class OrderedLinkedList<T>
         Iterator secondIterator = list2.secondIterator();
         ^
OrderedLinkedList.java:159: cannot find symbol
symbol  : method secondIterator()
location: class OrderedLinkedList<T>
         while(firstIterator.hasNext() && secondIterator().hasNext())
                                          ^
OrderedLinkedList.java:185: cannot find symbol
symbol  : variable resultList
location: class OrderedLinkedList<T>
               resultList.add(secondObject);
               ^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
16 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

Open in new window

0
 
LVL 4

Accepted Solution

by:
msk_apk earned 500 total points
ID: 35220882
i learned your code and understood it and i added some method. i am sure you know that.
OrderedLinkedList.java
TestProgOrderedLinkedList.java
0
 

Author Closing Comment

by:wjenlow
ID: 35232701
I still have a few errors but this solution was very helpful! Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now