-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 ?

11
  • You are passing the array correctly. Perhaps the signature of your method is wrong. Commented Nov 25, 2014 at 9:05
  • 1
    possible duplicate of pass array to method Java Commented Nov 25, 2014 at 9: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. Commented Nov 25, 2014 at 9: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. Commented Nov 25, 2014 at 9:07
  • 3
    Many bad programming practices in few lines of code ... Commented Nov 25, 2014 at 9:10

1 Answer 1

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;
}
}
Sign up to request clarification or add additional context in comments.

5 Comments

i tried it but i got error inside the method in return Statements
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.
@Reem , Change the void to int in private static void recursion(int k, int[] numbers, int low, int h) to make it work
@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.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.