Return question of Java 1D integer array

Asked 2 years ago, Updated 2 years ago, 28 views

public void getGradeMenu()
{
    int Scores[] = new int[StudentNum];
    while(MenuNum != 7)
    {
        showGradeMenu();
        MenuNum = scan.nextInt();
        switch(MenuNum)
        {
            case 1: getGradeArray(Scores);
                    break;
            case 2: getGradeList(Scores);
                    break;
            case 3: pickGradeList(Scores);
                    break;
            case 4: checkMinMaxGrade(Scores);
                    break;
            case 5: CheckAvgMinddleGrade(Scores);
                    break;
            case 6: sortGrade(Scores);
                    break;
            case 7: break;
            default : System.out.println ("No Program! ");
        }
    }
}
public void getGradeArray(int []Array)
{
    for(int i=0; i <Array.length; i++)
    {
        System.out.println ("Please enter your grade: ");
        Array[i] = scan.nextInt();
    }
}

You created a one-dimensional array in getGradeArray.

In my head, I think I should return the array after returning it, but if I return it, the price doesn't come out. I heard that the price doesn't go away if I return the array, but did I bring it in wrong? Recall from the method that checks the value of an array is useless by receiving new inputs, not just values.

What I don't understand more is that the above code didn't return, but the value went well with the Scores array. I don't understand if the Scores array has a value for an array that hasn't been returned by any means. I don't know if I'm wrong about the role of the parameters. I don't understand at all.

java

2022-09-20 22:05

2 Answers

Well, it's been a long time since I've used Java, so I don't really remember it, but to explain it like this and that, in general, in the case of integers and decimals, it's right to do as you say.

However, in the case of an array, when a value is passed as a reference variable, the address where the value is stored is passed, not the actual value.

So if you store a value in an array in a particular function, that value will still remain in the same address when you exit the function, so you can retrieve the value stored in the array from the function that has already ended without a return value.

I don't know if the analogy is appropriate, but for example,

Let's say a friend asked a questioner to lend him a book.

You can think of variables such as integers and decimals as giving copies of the book.

So whether you take notes or scribble on that copy, the book you have doesn't change anything.

On the other hand, transferring the arrangement to the parameter is to put the book in the locker and give the locker number.

Then, the friend will go to the locker that the questioner told me to take out the book and take a class, right?

After class, a friend will put the book back in the locker, and of course, there will be a note or a scribble left by the friend.

You might be confused by metaphors, but... You can understand it like this.


2022-09-20 22:05

To return an array from a method to another method in Java, first, we have to create an array in Java and store array elements than simply return to the caller method.

class Test{

  // // main method
  public static void main(String[] args) {

    // // read array from a method
    int[] a = readArray();

    // // display array elements
    for(int i=0; i < a.length; i++){
      System.out.print(a[i] + "\t");
    }
  }

  // // method to return array elements
  static int[] readArray(){
    int[] arr = {10,20,30,40};
    return arr;
  }

}


2022-09-20 22:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.