-7

Here is my code:-

int[] numbers = new int[5];

    for (int i = 0; i < numbers.length; i++)
    {
        System.out.println("Please enter number");
        numbers[i] = input.nextInt();

    }
    System.out.println("Please enter your key");
    int k =input.nextInt();
    int Low=numbers[0];
     int H=numbers[4];

      Recursion(k,numbers ,Low , H);

 }

how can i pass array to a method ? i got error in the last line

i tried to make the method void and do all those things inside method but i got error ?

Sudhir kumar
  • 549
  • 2
  • 8
  • 31
Reem
  • 11
  • 1
  • 7
  • You are passing the array correctly. Perhaps the signature of your method is wrong. – Eran Nov 25 '14 at 09:05
  • 1
    possible duplicate of [pass array to method Java](http://stackoverflow.com/questions/1610757/pass-array-to-method-java) – nunofmendes Nov 25 '14 at 09:06
  • 3
    "I got error in the last line" doesn't tell us *anything* about the error. We don't know what the signature of the (unconventionally named) `Recursion` method is, or what the error is, or what you're trying to achieve. Please give us more information. – Jon Skeet Nov 25 '14 at 09:07
  • 1
    You got error? No, you got a specific error with a message explaining what and where the error is. Read it. And post it, along with the relevant code if you don't understand it. – JB Nizet Nov 25 '14 at 09:07
  • @nunofmendes: It's not a duplicate of that question, in that the *call* here looks fine. Presumably the method declaration for `Recursion` is inappropriate for that call, but we can't tell that based on the code provided. – Jon Skeet Nov 25 '14 at 09:07
  • @JonSkeet You are right. Probably the Recursion is the wrong signature. – nunofmendes Nov 25 '14 at 09:09
  • @Reem Can you post more info about that? – nunofmendes Nov 25 '14 at 09:09
  • int Recursion(int k , int A[] , int lowIn , int HIND ){ int mid =lowIn + HIND /2; if (lowIn > HIND ) return -1; if(k==A[mid]) return mid; else if (k – Reem Nov 25 '14 at 09:09
  • @Reem you should show your full codes with method signature. – user3437460 Nov 25 '14 at 09:10
  • 3
    Many bad programming practices in few lines of code ... – Junaid Nov 25 '14 at 09:10
  • @Reem: This should be in the *question* - along with the error message - not in a comment. Please read http://tinyurl.com/stack-hints – Jon Skeet Nov 25 '14 at 09:17

1 Answers1

1

Java always passes arguments by values to method. In case of Arrays/Objects it passes by value as well but here in this case its value is a reference.

Following is the code snippet you can use to pass an array to a method and return it back again.

import java.util.HashMap;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
    int[] numbers = new int[5];
    Scanner input = new Scanner(System.in);

    for (int i = 0; i < numbers.length; i++) {
        System.out.println("Please enter number");
        numbers[i] = input.nextInt();
    }
    System.out.println("Please enter your key");
    int k = input.nextInt();
    int Low = numbers[0];
    int H = numbers[4];

    int[] recursiveResult = recursion(k, numbers, Low, H);

}

private static int[] recursion(int k, int[] numbers, int low, int h) {
    // TODO Auto-generated method stub
    // Some Operation

    return numbers;
}
}
Abhijeet Dhumal
  • 1,799
  • 13
  • 24
  • i tried it but i got error inside the method in return Statements – Reem Nov 25 '14 at 09:17
  • 1
    No, Java *never* uses pass-by-reference. It *always* uses pass-by-value, but the value passed for class types - including arrays - is a reference. There's a big difference between "pass a reference by value" and "pass an object by reference". Please don't add to the confusion of others by conflating the two. – Jon Skeet Nov 25 '14 at 09:18
  • @Reem , Change the `void` to `int` in `private static void recursion(int k, int[] numbers, int low, int h)` to make it work – Spikatrix Nov 25 '14 at 09:22
  • @JonSkeet yes I wanted to say the same. Java passes by value only but in case of Arrays and objects java passes references as the values of objects. – Abhijeet Dhumal Nov 25 '14 at 09:26
  • @user3835765: Right, so say that - *don't* say that it passes by reference, when it doesn't. Now your answer is just code, with no explanation - it's still not really *helpful*. – Jon Skeet Nov 25 '14 at 09:28