i need a code to merge two ordered list of objects of integers.the method should receive references to each of the list objects to be merged , and should return a reference to merged list object.
pls help....

Recommended Answers

All 5 Replies

Make an attempt, at least. If it "doesn't work" then post that code and describe, exactly, what "doesn't work" mean and provide all (and complete) compiler/error messages.

This is not a code service.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public  class ListMerge
{
  public static void main( String[] args)
	{
       ArrayList x = new ArrayList();
       ArrayList y = new ArrayList();
       x.add(new Integer(5));
       x.add(new Character('m'));
       x.add("merge");

       y.add(new Double(12.4));
       y.add(new Long( 12));
       System.out.println("Final list:");
       for(int a = 0; a < y.size(); a++)
       {
           x.add(y.get(a));
       }
       for(int a = 0; a < x.size(); a++)
       System.out.println(x.get(a));
    }
}

i have dine this.but i want to take input from user and then merge them.
how can i do that?
I am not able to understand the last part of giving the reference.

I thought you wanted lists of Integers? Where did you copy this from (not that it's very good, BTW). Simply use the "addAll" method from the Collections class, see the API docs. And, of course, put the code into it's own method (as the description says). Retuning a reference, is, obviously, returning the "new" list. Retreiving input from users is something else, entirely, and you already have another thread on that.

Now, at least try to do your homework.

public  class ListMerge
{
  public static void main( String[] args)
    {

       Scanner input = new Scanner(System.in);

      System.out.println ("Input length of arraylist 1:");
      int n = input.nextInt();
      ArrayList x = new ArrayList();
       ArrayList y = new ArrayList();
       for (int i = 0; i < n; i++)
        {
            System.out.println ("Input x[ " + i +"] :" );
             x.add(new Integer(i));

        }


      System.out.println ("Input length of arraylist 2:");
      int m = input.nextInt();


       for (int i = 0; i < m; i++)
        {
            System.out.println ("Input y[ " + i +"] :" );
            y.add(new Integer(i));

        }
       List<Integer> all = new ArrayList<Integer>();

      all.addAll(x);
      all.addAll(y);
      System.out.println(all);


  }
}

i coded this.
but its not taking the values from user.
pls tel me where did i go wrong????

Because you are only asking the user for a single value. And then "counting" and adding those counts to the lists. You, obviously, need to ask the user for the values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.