How can we add two array?

Ap compsci merging two sorted arraylists into 1 final sorted array list? 10 pts :)?

  • so idk how to write this loop for mergeLists (combining it into 1 arraylist) the rest r given. THANKS IN ADVANCE :D import java.util.ArrayList; public class MergeSortingArrayLists { public static ArrayList<Double> mergeLists ( ArrayList<Double> arr1, ArrayList<Double> arr2 ) { ArrayList <Double> answer = new //_____________ArrayList<Double>(); // write code here... mines is probably wrong for (int i = 0; i<arr2.size(); i++) { if(arr1.compareTo(arr2)>0) { answer.add(i); } else { i++; } } return answer; } // mergeLists public static void main (String[] args) { ArrayList<Double> list1 = new ArrayList<Double> (); ArrayList<Double> list2 = new ArrayList<Double> (); System.out.println ("\nSample solution: [FROM THE TEACHER]"); //Double[] arr1 = { 1.1, 3.3, 5.5, 5.6, 5.7, 5.7, 7.7, 9.9, 11.11, 13.13, 15.15, 17.17, 19.19, 21.21 }; Double[] arr1 = { 2.1, 2.2, 2.3, 2.4, 4.4, 6.6, 8.8, 12.12 }; for ( double next : arr1 ) list1.add (next); System.out.println ( "\nList1:" ); System.out.println ( list1 ); //Double[] arr2 = { 2.1, 2.2, 2.3, 2.4, 4.4, 6.6, 8.8, 12.12 }; Double[] arr2 = { 1.1, 3.3, 5.5, 5.6, 5.7, 5.7, 7.7, 9.9, 11.11, 13.13, 15.15, 17.17,19.19, 21.21 }; for ( double next : arr2 ) list2.add (next); System.out.println ( "\nList2:" ); System.out.println ( list2 ); //Merge the two lists: ArrayList<Double> finalList = mergeLists ( list1, list2 ); System.out.println ( "\nFinal sorted list:" ); System.out.println ( finalList ); System.out.println ("\nSizes of lists:" ); System.out.println ("Array #1: " + arr1.length ); System.out.println ("Array #2: " + arr2.length ); System.out.println ("Final: " + finalList.size () ); } // main } // MergeSortingArrayLists

  • Answer:

    The code for this assignment may get you some points, but the class is wasted on you if you don't learn how to translate an algorithm into code. Surely the prof has described the merge algorithm, or pointed you at where you can read about it. It's simple. To merge two sorted lists (they *must* be sorted), simply get the first elements of both lists and loop until both lists are exhausted: elemA = (first item from list A) or "null" if list A is empty elemB = (first item from list B) or "null" if list B is empty while ( neither elemA nor elemB is 'null") ... if (elem A < elemB) then ... ... copy elemA to output list ... ... elemA = (next item from list A) or "null" if no more exist ... else ... ... copy elemB to output list ... ... elemB = (next item from list B) or "null" if no more exist ... end if end while That's the meat of the algorithm. Each iteration outputs the smaller of the two elements and replaces it with the nest element from the same list. Very simple, but there's some cleanup needed. At the end of the main loop, at least on list has been entirely copied to the output, but there may be uncopied elements in the other list. So, finish with: while elemA is not "null" ... copy elemA to the output list ... set elemA to next element from list A, or "null" if no more end while And do the same for elemB and list B in a separate loop. At most one of them will actually execute. (Why?) Quiz question: Under what conditions can you finish the main loop with BOTH elemA and elemB.empty?

kiki at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

dude you asked me to help you but in not there yet....

Bob

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.