0

I'm trying to call a void method that's having an array passed to it. I believe my syntax for the array being passed into it is incorrect, but I can't seem to determine the correct syntax. The literature I've been through so far tells me to use that array argument in that way, so if anyone could tell me the proper syntax for an array passing into a called method, I'd really appreciate it.

import java.util.Scanner;

public class Practice {
public static void main(String[] args){
    Scanner input = new Scanner(System.in);

    System.out.println("Enter your minimum number: ");
    int min = input.nextInt();

    System.out.println("Enter your maximum number: ");
    int max = input.nextInt();

    int[] numbers = new int[max-min];

    readNumbers(min, max, numbers[]);// the error is on this line with the numbers array
}// end of main method

public static void readNumbers(int min, int max, int[] array){
    for(int i=min;i<max;i++){
        array[i]=i;
    }// end of for loop
}// end of readNumbers method


}// end of class
  • remove the [], just readNumbers(min, max, numbers); I know you are just starting out, by try and avoid doing all the work in main and having static methods everywhere. – John Powell Oct 31 '14 at 12:15

2 Answers2

1

Remove the brackets from this line:

 readNumbers(min, max, numbers[]);

to

 readNumbers(min, max, numbers);
habitats
  • 2,203
  • 2
  • 23
  • 31
0

You do like this:

    private void PassArray(){
        String[] arrayw = new String[4]; //populate array
        PrintA(arryw); }

    private void PrintA(String[] a){
        //do whatever with array here 
}

Just pass it as other variable. In Java, Arrays are passed by reference

refrence :duplicate link

Community
  • 1
  • 1