Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need help merging arraylists and sorting

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help merging arraylists and sorting

    First time posting here and I'm looking for a little bit of help. Basically what I need to do is develop an algorithm and than code it.

    The situation is I need to create two array lists, one 30 numbers and the other 20 (the array lists are full of random numbers), and add them into one array in order from either lowest to highest, or highest to lowest. I can easily figure out what to do with the algorithm, and I know how to add them together, but the trouble I have is adding them in order from low/high or high/low.

    I could easily just use the ArrayList add() method and than sort them, but my professor would like it to sort them as each individual number is added. I don't have any code ATM as I'm just looking for some guidance rather than the answer. Not gonna learn with just the answer. Any help is appreciated. I was looking towards using a for loop but not sure in what way.

    Thanks for any help ahead of time


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help merging arraylists and sorting

    This looks almost exactly like the merge sort algorithm. Merge sort - Wikipedia, the free encyclopedia

    The main part you need is the merge function. It takes 2 lists that are in sorted order (from you're description it looks like you'll need to sort those two lists first, use which ever method you like best), then goes through each list, remove the smaller (or larger, depending on if you're sorting lower-to-higher or higher-to-lower) of the two elements at the front of the lists, then add that element to the front of your new list. Repeat until both lists are empty. If one list is empty, you can "dump" the remaining sorted items in the other list into the end of your new list.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help merging arraylists and sorting

    import java.util.*;
    import java.awt.*;
     
    public class ArrayListRandom
    {
      public static void main(String[] args)
      {
       //New ArrayLists to store the random integers
       ArrayList <Integer> list1 = new ArrayList <Integer>(30);
       ArrayList <Integer> list2 = new ArrayList <Integer>(20);
       ArrayList <Integer> list = new ArrayList <Integer>(50);
       //Random generator
       Random ran = new Random();
       int g = ran.nextInt(50);
       int f = ran.nextInt(50);
       //Adding the randomly generated numbers needed to start the lists
       list1.add(g);
       list2.add(f);
       //Printing out numbers given
       System.out.println(g);
       System.out.println(f);
       //Empty integers to store for later use
       int i = 0;
       int k = 0;
       int q = 0;
     
       //For loop to store the 30 random numbers in order
       for (i = 0; i < 30; i++)
       {
         g = ran.nextInt(50);
         int a = 0; 
         System.out.println(g);
         while ((a < list1.size()) && (g > list1.get(a)))
         {
         a++;
         }
         list1.add(a,g);
         }
     
       //For loop to store the 20 random numbers in order
       for (k = 0; k < 20; k++)
       {
         f = ran.nextInt(50);
         int a = 0; 
         System.out.println(f);
         while ((a < list2.size()) && (f > list2.get(a)))
         {
         a++;
         }
         list2.add(a,f);
         }
     
       for (q = 0; q < 50; q++)
       {
         int a = 0;
         list.add(a,list1.get(0));
         while ((a < list.size() && (q > list.get(a))))
         {
           a++;
         }
         if (list1.get(a) > list2.get(a))
         {
           list.add(a,list1.get(a));
         }
         //else
         //{
          // list.add(list2.get(a));
         //}
       }
         System.out.println(list1);
         System.out.println(list2);
         System.out.println(list);
      }
    }

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help merging arraylists and sorting

    That is the code I have so far.

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help merging arraylists and sorting

    Quote Originally Posted by helloworld922 View Post
    This looks almost exactly like the merge sort algorithm. Merge sort - Wikipedia, the free encyclopedia

    The main part you need is the merge function. It takes 2 lists that are in sorted order (from you're description it looks like you'll need to sort those two lists first, use which ever method you like best), then goes through each list, remove the smaller (or larger, depending on if you're sorting lower-to-higher or higher-to-lower) of the two elements at the front of the lists, then add that element to the front of your new list. Repeat until both lists are empty. If one list is empty, you can "dump" the remaining sorted items in the other list into the end of your new list.
    A lot of that looks like foreign language to me and I was wondering if their was any way I could do it in just a quick for loop, as I posted shortly after you.

Similar Threads

  1. [SOLVED] Sorting and Merging
    By javapenguin in forum What's Wrong With My Code?
    Replies: 36
    Last Post: December 7th, 2010, 08:04 PM
  2. [SOLVED] Update on "Sorting and Merging"
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 7th, 2010, 07:45 PM
  3. Encapsulation question regarding ArrayLists
    By LDM91 in forum Collections and Generics
    Replies: 3
    Last Post: October 27th, 2010, 11:51 AM
  4. Transferring elements between arraylists
    By KipTheFury in forum Collections and Generics
    Replies: 6
    Last Post: August 23rd, 2010, 02:13 PM
  5. [SOLVED] How to dynamically create ArrayLists (or something similar)?
    By igniteflow in forum Collections and Generics
    Replies: 4
    Last Post: August 6th, 2009, 06:00 AM