如何在void方法中调用多个数组?

时间:2019-03-18 16:42:49

标签: java arrays methods

我对如何调用我的方法感到困惑。 我收到找不到符号(playerName,battingArray)的错误消息。 -我打错了吗? -是否设置了正确填充数组的方法?

我什至不确定这是否是执行此操作的最佳方法。

任何帮助都是如此!

//Import util to accept input
import java.util.Scanner;


    public static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {

        //Assign number of players to variable and call method readNumPlayers
        int numPlayers = readNumPlayers();
        System.out.println(numPlayers);

        //Call readData method
        readData(playerName, numPlayers, battingArray);

}//end main

    //readNumPlayers method
    public static int readNumPlayers() {
        System.out.println("Number of players: ");
        int numPlayers = input.nextInt();
        if (numPlayers <=0) {
            System.out.println(" Error- Try again ");
            System.out.println("Number of players: ");
            numPlayers = input.nextInt();
        }//end catch to ensure entry is positive integer
        return numPlayers;  //return
    }//end readNumPlayers


    //readData method
    public static void readData(String[] playerName, int numPlayers,
                                double[] battingArray) {
        playerName = new String[numPlayers];
        System.out.println("Enter player's last name: ");
        for (int i = 0; i < playerName.length; i++) {
            playerName[i] = input.next();
        }  //end for loop

        battingArray = new double[numPlayers];
                System.out.println("Enter player's batting average: ");
                for (int i = 0; i < battingArray.length; i++) {
            battingArray[i] = input.nextDouble();
        }// end for loop


      }//end readData

}

1 个答案:

答案 0 :(得分:0)

当您将数组作为参数传递时,readData的调用方会提供 引用填充数组,同时您可能会影响内容所引用数组中的内容,您不能更改引用本身,因此您不应在方法中分配数组-例如,您的行:
battingArray = new double[numPlayers];

分配新数组并填写该数组时,调用者无法访问该数组,并且您也不能在调用者传递的数组中放入任何内容(并且可以可以访问) 只需使用呼叫者传入的double[] battingArray参数即可。

这是一个非常简单的示例的两个版本;在第一个readData方法中分配它自己的数组并填充它。您会看到,当main打印数组内容时,您会得到null,因为readData填充的数组不是传递的数组相同。

在第二个中,readData只是将值放入调用者传递的数组中。

第一:

import java.util.Scanner;

class Main
{
  public static void main(String[] args)
  {
    int count = 2;
    String[] names = new String[count];
    double[] avgs = new double[count];
    readData(names, avgs, count);
    System.out.println("names[0] in main: " + names[0]);
  }

  public static void readData(
    String[] playerNames,
    double[] battingAvg,
    int numPlayers
  )
  {
    Scanner in = new Scanner(System.in);
    // allocating a new array
    String[] playerNames2 = new String[numPlayers];
    for (int i = 0; i < numPlayers; i++)
    {
      System.out.print("Players name: ");
      String line = in.nextLine();
      // putting the value into the newly allocated array
      playerNames2[i] = line.trim();
    }
    System.out.println("playerNames2[0] in readData: " + playerNames2[0]);
  }
}

第二:

import java.util.Scanner;

class Main
{
  public static void main(String[] args)
  {
    int count = 2;
    String[] names = new String[count];
    double[] avgs = new double[count];
    readData(names, avgs, count);
    System.out.println("names[0] in main: " + names[0]);
  }

  public static void readData(
    String[] playerNames,
    double[] battingAvg,
    int numPlayers
  )
  {
    Scanner in = new Scanner(System.in);
    // no array allocated this time
    for (int i = 0; i < numPlayers; i++)
    {
      System.out.print("Players name: ");
      String line = in.nextLine();
      // put the value into the array that was passed
      playerNames[i] = line.trim();
    }
    System.out.println("playerNames[0] in readData: " + playerNames[0]);
  }
}

由于调用方告诉您数组的大小(numPlayers参数),所以假设为“ 10”,如果调用方的数组实际上不够大(例如,他们做了new String[5]),那么可能会得到ArrayIndexOutOfBoundsException,所以readData可能想抓住它并返回而没有完成循环。

(我创建了一个Java repl,您可以在其中看到版本2)