在JAVA中,如何从一个方法访问对象数组到另一个方法?

在JAVA中,如何从一个方法访问对象数组到另一个方法?,java,class,oop,methods,Java,Class,Oop,Methods,我已经创建了一个对象数组,它是从一个名为dataInput()的方法创建的。我想调用main方法中的persons数组。提前谢谢 public static void dataInput() { Scanner input = new Scanner(System.in); int noOfPersons = 3; String name = ""; int no = 0; String city = "";

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

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;
    }
}
publicstaticvoiddatainput(){
扫描仪输入=新扫描仪(System.in);
int noOfPersons=3;
字符串名称=”;
int no=0;
字符串城市=”;
输入项[]人=新输入项[3];
人员[0]=新输入(姓名、编号、城市);
人员[1]=新输入(姓名、编号、城市);
人员[2]=新输入(姓名、编号、城市);
for(int i=0;i
公共静态输入[]数据输入(){
扫描仪输入=新扫描仪(System.in);
int noOfPersons=3;
字符串名称=”;
int no=0;
字符串城市=”;
输入项[]人=新输入项[3];
人员[0]=新输入(姓名、编号、城市);
人员[1]=新输入(姓名、编号、城市);
人员[2]=新输入(姓名、编号、城市);
for(int i=0;i
现在在main方法中,
inputTry[]persons=datainput()
将获取方法返回的数组。

publicstaticvoidmain(String[]args){
Inputry[]人=数据输入();
}
公共静态输入[]数据输入(){
扫描仪输入=新扫描仪(System.in);
int noOfPersons=3;
字符串名称=”;
int no=0;
字符串城市=”;
输入项[]人=新输入项[3];
人员[0]=新输入(姓名、编号、城市);
人员[1]=新输入(姓名、编号、城市);
人员[2]=新输入(姓名、编号、城市);
for(int i=0;i
您应该这样做,以便在调用方法中使用返回值。

  • 如果
    dataInput()
    main属于同一类 可以“在类的开头”将此数组声明为全局数组

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


公共静态void dataInput(){
//你的代码
输入项[]人=新输入项[3];
getPersonsArray(名称、编号、城市);
//你的代码
}
公共静态输入项[]getPersonsArray(字符串名称、整数编号、字符串城市){
人员[0]=新输入(姓名、编号、城市);
人员[1]=新输入(姓名、编号、城市);
人员[2]=新输入(姓名、编号、城市);
返回人员;
}

您可以使
dataInput
返回数组,这样您就可以在调用方法中访问它。注意java命名约定。变量名应以小写字符开头,类名应以大写字符开头。
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;
}