Java Method Call Array

2019-09-20 15:14发布

How do I pass an array from my main method to another method? I'm having an error with the parameters. Do I use the return value from main? And since the return value from main is an array, should the parameters for the call of main have brackets? Should there be a value between those brackets?

public class arraysAndMethods {

public void printArray(double[] arr) {
    int x = arraysAndMethods.main(double[i] arr);//error with paremeters
    for (int i = 0; i < studGrades.lenght; i++)
        System.out.print(studGrades[i] + " ");
}// end of printArray method

public static double[] main(String args[]){// double array
    java.util.Scanner input = new java.util.Scanner(System.in); // input scanner
    System.out.println("What is the size of the class?");
    int n = input.nextInt();
    double[] arr = new double[n];// declare and initialize array to have n many elements
    for (int i = 0; i < arr.length;i++) {// input grades for each students
        System.out.println("What is the grade of student #" + (i+1));
        arr[i] = input.nextDouble();
    } // end of for loop
    return arr; 
}// end of main method

}// end of class

4条回答
该账号已被封号
2楼-- · 2019-09-20 15:26

Your main method is where the program begins execution. In other words you should be calling printArray() from within main, not the other way around

查看更多
在下西门庆
3楼-- · 2019-09-20 15:27

This whole thing doesn't make much sense. If you're hoping to have this as the main method of a Java program, this won't work because the main method must have a void return type. Regardless, you have a syntax error here:

int x = arraysAndMethods.main(double[i] arr);//error with paremeters

Instead it should be

int x = arraysAndMethods.main(arr);//error with paremeters

as the variable has already been declared. I'm not sure what you were trying to do with double[i], but that syntax is more like a declaration, i.e.

double[] someArrayName = new double[5];
查看更多
可以哭但决不认输i
4楼-- · 2019-09-20 15:29

Just pass the name, not the type.

int x = arraysAndMethods.main(arr);

EDIT: Besides that, your code shows a few other problems.

  1. main(...) is the entry point to your application. It doesn't make sense to call main(...) from the other method. It should be the other way around.
  2. main(...) HAS TO have the following signature: public static void main(String[] args). You cannot declare it to return an array of double.
查看更多
Emotional °昔
5楼-- · 2019-09-20 15:39

I think I see where the confusion is. Perhaps this will help.
First I think you should understand something very important about Java.
Your main method is the FIRST method that is run.

So let's give a short walk through of how a Java program is created without an IDE. First, you write your code in a text file and rename it to arraysAndMethods.java when you're done. Next we need to take your code and put it into a form that is usable by the computer it is first compiled down to bytecode with the following from the command line:
javac arraysAndMethods.java
After it is compiled you can run the program with this command:
java arraysAndMethods
Your program will run if there are no problems. You say that you want to pass variables things into the main method? Here is how you do that from the command line:
java arraysAndMethods 45.6 38.2 5.5 105.3
Looking at your main method, it takes the following arguments: (String args[])
So in your case, the 45.6 38.2 5.5 105.3 would be passed into your main method as an array of strings. With the first entry being 45.6, followed by 38.2, then 5.5 and finally 105.3. It would look like this in the array: ["45.6"],["38.2"],["5.5"],["105.3"] but they are all Strings and not doubles.

All of this is to say that if you want to pass something in to your main method, you'll either need to do it through the command line, or look up how your individual IDE (i.e. Eclipse, Netbeans, etc.) handles that.

So to recap: The parameters in the main method are coming in from the console unless other specifications are made, and in your case it is returning an array of type double.

I know this is quite verbose, but bear with me, I'm almost done.
When a Java program is run(I'm simplifying a bit here), it comes into the main method and executes the first line it sees. When it's done with that one, it goes onto the next line, and so forth until it gets to the end of the main method. Then the program is done.
So everything MUST be done in the main method. Although you can call other Classes and methods from the main method.
Now that you have the explanation, here is what I would do to fix your code:

public class arraysAndMethods {//Changed from arraysAndMethods to ArraysAndMethods because classes are always capitalized

public static void printArray(double[] arr) {//Added static so this could be used within the same class as the main method.
    //int x = arraysAndMethods.main(double[i] arr); No need for this line
    for (int i = 0; i < arr.length; i++)// Changed "studGrades.lenght" to arr.length
        System.out.print(arr[i] + " ");//Changed "studGrades" to arr
}// end of printArray method

public static void main(String args[]){// Changed the return type to "void". No need to return the double array.
    java.util.Scanner input = new java.util.Scanner(System.in); // input scanner
    System.out.println("What is the size of the class?");
    int n = input.nextInt();
    double[] arr = new double[n];// declare and initialize array to have n many elements
    for (int i = 0; i < arr.length;i++) {// input grades for each students
        System.out.println("What is the grade of student #" + (i+1));
        arr[i] = input.nextDouble();
    } // end of for loop
    printArray(arr);
    //return arr; No need for this line 
}// end of main method

}// end of class

Please be sure to mark the best answer when you're ready and up-vote any answers or comments you feel helped. Not just for this question, where you are the original poster, but for other question threads as well.

查看更多
登录 后发表回答