【问题标题】:How to access an Array of Objects from one method to other method in JAVA?如何在 JAVA 中从一种方法访问对象数组到另一种方法?
【发布时间】:2021-08-11 12:57:50
【问题描述】:

我已经创建了一个对象数组,它是从一个名为 dataInput() 的方法创建的。我想在 main 方法中调用 people 数组。提前致谢。

public static void dataInput() {

    Scanner input = new Scanner(System.in);

    int noOfPersons = 3;

    String name = "";
    int no = 0;
    String city = "";

    inputTry[] persons = new inputTry[3];
    persons[0] = new inputTry(name, no, city);
    persons[1] = new inputTry(name, no, city);
    persons[2] = new inputTry(name, no, city);

    for (int i = 0; i < noOfPersons; i++) {
        System.out.print("Enter person name: ");
        name = input.nextLine();
        System.out.print("Enter person number: ");
        no = input.nextInt();
        input.nextLine();
        System.out.print("Enter where the person lives: ");
        city = input.nextLine();

        persons[i].name = name;
        persons[i].no = no;
        persons[i].city = city;
    }
}

【问题讨论】:

  • 可以让dataInput返回数组,这样就可以在调用方法中访问了。
  • 注意 java 命名约定。变量名应以小写字母开头,类名应以大写字母开头。

标签: java class oop methods


【解决方案1】:
  • 如果 dataInput()ma​​in 属于同一类
    您可以在“类的开头”声明这个数组全局。

  • 如果 dataInput() 不在 ma​​in 类中
    您可以创建一个名为 getPersonsArray() 的公共方法,并从 dataInput() 调用它。现在你可以从这个类中获取对象并调用方法getPersonsArray()


  public static void dataInput() {
     // your code
     inputTry[] persons = new inputTry[3];
     getPersonsArray(name, no, city);
    // your code
  }

    public static inputTry[] getPersonsArray(String name,int no,String city) {
       persons[0] = new inputTry(name, no, city);
       persons[1] = new inputTry(name, no, city);
       persons[2] = new inputTry(name, no, city);

       return persons;
    }

【讨论】:

    【解决方案2】:
    public static void main(String[] args) {
        inputTry[] persons = dataInput();
    }
    
    public static inputTry[] dataInput() {
    
        Scanner input = new Scanner(System.in);
    
        int noOfPersons = 3;
    
        String name = "";
        int no = 0;
        String city = "";
    
        inputTry[] persons = new inputTry[3];
        persons[0] = new inputTry(name, no, city);
        persons[1] = new inputTry(name, no, city);
        persons[2] = new inputTry(name, no, city);
    
        for (int i = 0; i < noOfPersons; i++) {
            System.out.print("Enter person name: ");
            name = input.nextLine();
            System.out.print("Enter person number: ");
            no = input.nextInt();
            input.nextLine();
            System.out.print("Enter where the person lives: ");
            city = input.nextLine();
    
            persons[i].name = name;
            persons[i].no = no;
            persons[i].city = city;
        }
    
        return persons;
    }
    

    你应该这样做,这样你就可以在调用方法中使用返回值。

    【讨论】:

      【解决方案3】:
      public static inputTry[] dataInput() {
      
          Scanner input = new Scanner(System.in);
      
          int noOfPersons = 3;
      
          String name = "";
          int no = 0;
          String city = "";
      
          inputTry[] persons = new inputTry[3];
          persons[0] = new inputTry(name, no, city);
          persons[1] = new inputTry(name, no, city);
          persons[2] = new inputTry(name, no, city);
      
          for (int i = 0; i < noOfPersons; i++) {
              System.out.print("Enter person name: ");
              name = input.nextLine();
              System.out.print("Enter person number: ");
              no = input.nextInt();
              input.nextLine();
              System.out.print("Enter where the person lives: ");
              city = input.nextLine();
      
              persons[i].name = name;
              persons[i].no = no;
              persons[i].city = city;
          }
          return persons;
      }
      

      现在在您的 main 方法中,inputTry[] persons=datainput() 将获取您的方法返回的数组。

      【讨论】:

        猜你喜欢
        • 2020-06-16
        • 1970-01-01
        • 2020-02-20
        • 2016-02-11
        • 2011-07-05
        • 2015-06-15
        • 2015-12-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多