0

Hellp guys.

Im trying to do this following example.

What this program does, is that it joins and sorts two lists in the merge-method, and returns it to the main method.

however I'm running into issues while trying to work inside my method!

--> merge(int[],int) in Exercise07_31 cannot be applied to (int[],int[]) --> Exercise07_31.java:62: int cannot be dereferenced

and a bunch of more errors...

I don't seem to figure out where the error occurs, it should really be one little thing that i forgot. Isn't arrays integers, if so, how do i convert them and use them right ?

Can anyone please help me out? Thanks!

import java.util.*;

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

     Scanner input = new Scanner(System.in);

     //Prompt for user input list 1:

     System.out.print("Enter list1: ");
     int a1 = input.nextInt();

     //Set array:
     int[] list1 = new int[a1];

     //Prompt for array input:
     for (int k = 0; k < a1; k++)
     {
     list1[k] = input.nextInt();;      
     }

     //Prompt for user input list 2: 

     System.out.print("Enter list2: ");
     int a2 = input.nextInt();

     //Set array
     int[] list2 = new int[a2];

     //Prompt for array input
     for (int j = 0; j < a2; j++)
     {
     list2[j] = input.nextInt();;      
     }     

     System.out.println("The merged list is ");

     for (int l = 0; l < a3; l++) 
     {
     System.out.print(merge(list1, list2) + ", ");
     }

}

public static int[] merge(int[] list1, int list2)

{

     int a3 = list1.length + list2.length;

     //Creating array for join:
     int[] list3 = new int[a3];

     //join the lists to list3

     for (int s = 0; s < list1.length; s++)
     {
     list3[s] = list1[s];
     }
     for (int h = 0; h < list2.length; h++)
     {
     list3[list1.length + h] = list2[h];
     }


     //Sort the lists:


     int temp = 0; 
     for (int j = 0; j < list3.length; j++)
     {

     for (int k = 0; k < list3.length - 1; k++)
     {
       if(list3[k + 1] < list3[k])
       {
       temp = list3[k];
       list3[k] = list3[k + 1];
       list3[k + 1] = temp;
       }   
     }
     }
     return list3;
}
}
Avacay
  • 153
  • 1
  • 13

1 Answers1

0
merge(int[] list1, int list2)

accepts an array as its first parameter (int[]) and a single int as the second. You want it to accept two arrays, so its signature needs to be

merge(int[] list1, int[] list2)
drew moore
  • 31,565
  • 17
  • 75
  • 112
  • Anyway, is there an easier way to join two arrays instead of a for loop ? – Avacay Oct 30 '14 at 23:49
  • @Avacay essentially no: http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java – zapl Oct 30 '14 at 23:51
  • @zapl i see ! Thank you for the reply.. Well as for now, i corrected my code, and it compiles well! However my println seems to be in matrix format :( what does this mean ? – Avacay Oct 30 '14 at 23:53
  • @Avacay do you see something like http://stackoverflow.com/questions/1040868/java-syntax-and-meaning-behind-b1ef9157-binary-address ? You also `merge` the arrays in a loop. You should only merge once and then print the content with a loop – zapl Oct 31 '14 at 09:37