2

here is the whole program:

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 int merge(ArrayList<int> x, ArrayList<int> y) 
  {
     List<Integer> all = new ArrayList<Integer>();

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

   return all;
  }

}

also tell me how do i call the function merge?

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
bhavna raghuvanshi
  • 1,035
  • 4
  • 16
  • 16
  • 1
    how do you want to merge them? – Chris J Jun 16 '10 at 11:14
  • Nitpicking: note, that the Java compiler will complain, if you try to use a collection of element type `int`, since `int` is a primitive type, and primitive types cannot be used with generics. – Dirk Jun 16 '10 at 11:26
  • possible duplicate of (same poster, same vague question) [how to merge two ordered list of objects?](http://stackoverflow.com/questions/3051603/how-to-merge-two-ordered-list-of-objects) – polygenelubricants Jun 16 '10 at 11:26
  • No, the real question is in the title. In that question he asked how to merge lists, in this one - how to pass an arraylist to a method. – bezmax Jun 16 '10 at 11:30

3 Answers3

1

You would just declare a method like this:

List<YourObjectType> mergeArrays(ArrayList<YourObjectType> array1, ArrayList<YourObjectType> array2) {

   List<YourObjectType> mergedArray = new ArrayList<YourObjectType>();

   // perform merge

   return mergedArray;
}
dcp
  • 54,410
  • 22
  • 144
  • 164
1

The method signatur could be like this:

public static List<YourType> merge(List<YourType> firstList, List<YourType> secondList);

The returned list will be created in the method body and filled with the content of the source lists.


Your code shouldn't compile (but it might be a copy/paste/convert problem). This signature would be syntactically correct:

List<int> merge(ArrayList<int> x, ArrayList<int> y) { ... }

Can't prove it here but it may be a problem with In- and Outboxing. Try this signature instead:

List<Integer> merge(ArrayList<Integer> x, ArrayList<Integer> y) { ... }

and use this declaration in the main methods body:

  ArrayList<Integer> x = new ArrayList<Integer>();
  ArrayList<Integer> y = new ArrayList<Integer>();

and finally - you're not calling the merge method at all in your code. I'm missing something like

ArrayList<Integer> result = new ListMerge().merge(x,y);

at the very end of the main method body...

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
1

The method as you have written it is expecting to return an int, try:

public List<Type> mergeLists(List<Type> a, List<Type> b) {
    List<Type> all = new ArrayList<Type>();
    all.addAll(a);
    all.addAll(b);

    return all;
}

If you want to merge differently, e.g. if there should be some sort of sorting or avoidance of duplicates, then you need to change the addAlls to whatever you need.

Call the method by doing:

List<Type> mergedList = mergeLists(x, y);
DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
  • What's not working? You do realise that everywhere I wrote 'Type' in my answer I was expecting you to replace it with the type of the objects you are dealing with, which based on the update to your question I guess is Integer. – DaveJohnston Jun 16 '10 at 11:39
  • Updated what, the code or your question? Your question does ArrayList which is not possible, you can't use primitive types. Instead use ArrayList – DaveJohnston Jun 16 '10 at 11:42
  • it says: unexpected type found:int required:reference – bhavna raghuvanshi Jun 16 '10 at 11:42
  • Does the code that you are trying to run actually reflect what is in your question? If so then you need to change the return type. You have "list int merge(ArrayList x, ArrayList y)" but you need to change it to "List merge(ArrayList x, ArrayList y)" – DaveJohnston Jun 16 '10 at 11:45
  • I think that is another question then, but I don't see why it won't accept input, what happens when you run it? – DaveJohnston Jun 16 '10 at 11:49
  • it is running successfully. it doesnt allow user to give input. it assumes values to be 0,1,2..... – bhavna raghuvanshi Jun 16 '10 at 11:52
  • its output is like this: Input length of arraylist 1: 3 Input x[ 0] : Input x[ 1] : Input x[ 2] : Input length of arraylist 2: 3 Input y[ 0] : Input y[ 1] : Input y[ 2] : [0, 1, 2, 0, 1, 2] – bhavna raghuvanshi Jun 16 '10 at 11:55
  • But that's what your code does. What do you expect it to do? You are asking the user to input the size of the array list, not the actual values that go in it. If you want the user to specify the actual values you will need to add another call to nextInt within each of the loops. – DaveJohnston Jun 16 '10 at 12:00