Java 如何从一个方法返回多个数组?

Java 如何从一个方法返回多个数组?,java,arrays,Java,Arrays,假设我想将6个数组从一个方法返回到另一个方法(在另一个类中)。 这样做的最佳方式是什么?为什么 这就是我目前所拥有的 public Object getData()throws FileNotFoundException{ counter = 0; Scanner f = new Scanner(new File("Contacts.txt")).useDelimiter(","); while (f.hasNext()){ firstNames[cou

假设我想将6个数组从一个方法返回到另一个方法(在另一个类中)。 这样做的最佳方式是什么?为什么

这就是我目前所拥有的

 public Object getData()throws FileNotFoundException{
    counter = 0;
    Scanner f = new Scanner(new File("Contacts.txt")).useDelimiter(",");
    while (f.hasNext()){
        firstNames[counter] = f.next();
        lastNames[counter] = f.next();
        emailList[counter] = f.next();
        ageList[counter] = f.next();
        imgLoc[counter] = f.nextLine();
        counter++;
    }
    f.close();

    firstNames = Arrays.copyOf(firstNames, counter);
    lastNames = Arrays.copyOf(lastNames,counter);
    emailList = Arrays.copyOf(emailList, counter);
    ageList = Arrays.copyOf(ageList, counter);
    imgLoc = Arrays.copyOf(imgLoc, counter);
    data = Arrays.copyOf(data, counter);
    for (int i = 0; i <= counter - 1; i++){
        data[i] = firstNames[i] + " " + lastNames[i] + ", " + ageList[i];
    }
    ArrayList<Object> arrays = new ArrayList<Object>();
    arrays.add(firstNames);
    arrays.add(lastNames);
    arrays.add(emailList);
    arrays.add(ageList);
    arrays.add(imgLoc);
    arrays.add(data);

    return arrays;
}
public Object getData()引发FileNotFoundException{
计数器=0;
Scanner f=新的扫描仪(新文件(“Contacts.txt”)。使用分隔符(“,”);
while(f.hasNext()){
名字[计数器]=f.next();
lastNames[counter]=f.next();
emailList[计数器]=f.next();
ageList[计数器]=f.next();
imgLoc[counter]=f.nextLine();
计数器++;
}
f、 close();
firstNames=Arrays.copyOf(firstNames,counter);
lastNames=Arrays.copyOf(lastNames,counter);
emailList=Arrays.copyOf(emailList,计数器);
ageList=Arrays.copyOf(ageList,计数器);
imgLoc=Arrays.copyOf(imgLoc,计数器);
数据=数组.copyOf(数据,计数器);

对于(inti=0;i我认为这是一个糟糕的想法

我更喜欢一个对象,它将first、last、email、age和image封装到Person类中并返回它们的列表

Java是一种面向对象的语言。如果你停止用字符串、整数和数组等基本体来思考问题,而开始用对象来思考问题,你会做得更好。只要你能,就把本来应该放在一起的东西封装成一个单一的对象

下面是我如何编写该方法的:

public List<Person> readPersons(File file) throws FileNotFoundException {

    List<Person> persons = new LinkedList<Person>();

    Scanner f = new Scanner(file).useDelimiter(",");
    while (f.hasNext()){
        String first = f.next();
        String last = f.next();
        String email = f.next();  
        String age = f.next(); // age ought to be a positive integer
        String imageLocation = f.nextLine();
        persons.add(new Person(first, last, email, age, imageLocation));
    }

    return persons;
}
public List readers(文件)抛出FileNotFoundException{
列表人员=新建LinkedList();
Scanner f=新的扫描仪(文件).useDelimiter(“,”);
while(f.hasNext()){
字符串first=f.next();
字符串last=f.next();
字符串email=f.next();
字符串age=f.next();//age应该是正整数
字符串imageLocation=f.nextLine();
添加(新的人(第一个、最后一个、电子邮件、年龄、图像位置));
}
返回人员;
}
代码更少,更容易理解。

更干净的实现(尽管缺少错误检查…)

公共类人物{
私有最终字符串fName;
私有最终字符串lName;
私人最终字符串电子邮件;
私人最终年龄;
私有最终字符串imgLoc;
公众人物(字符串fName、字符串lName、字符串电子邮件、字符串年龄、,
字符串(imgLoc){
超级();
this.fName=fName;
this.lName=lName;
this.email=电子邮件;
这个。年龄=年龄;
this.imgLoc=imgLoc;
}
/*…这里的人*/
}
公共对象getData()引发FileNotFoundException{
ArrayList out=新的ArrayList();
Scanner f=新的扫描仪(新文件(“Contacts.txt”)。使用分隔符(“,”);
while(f.hasNext()){
添加(新的人)(
f、 next(),
f、 next(),
f、 next(),
f、 next(),
f、 next());
}
f、 close();
返回;
}

以下是我要做的:

public static class Person {
    public final String firstName, lastName, email, age, imgLoc;

    Person(String firstName, String lastName, String email, String age, String imgLoc) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.age = age;
        this.imgLoc = imgLoc;
    }
}

public List<Person> getData() throws FileNotFoundException {
    ArrayList<Person> list = new ArrayList<Person>();
    Scanner f = new Scanner(new File("Contacts.txt")).useDelimiter(",");
    while (f.hasNext()) {
        list.add(new Person(f.next(), f.next(), f.next(), f.next(), f.nextLine()));
    }
    f.close();
    return list;
}
公共静态类人物{
公共最终字符串firstName、lastName、email、age、imgLoc;
Person(String firstName、String lastName、String email、String age、String imgLoc){
this.firstName=firstName;
this.lastName=lastName;
this.email=电子邮件;
这个。年龄=年龄;
this.imgLoc=imgLoc;
}
}
public List getData()引发FileNotFoundException{
ArrayList=新建ArrayList();
Scanner f=新的扫描仪(新文件(“Contacts.txt”)。使用分隔符(“,”);
while(f.hasNext()){
添加(新人物(f.next(),f.next(),f.next(),f.next(),f.nextLine());
}
f、 close();
退货清单;
}
更新:在我写这篇文章时,claymore1977发布了一个几乎相同的答案。唯一值得注意的区别是,我将方法的类型声明为List,而他保留了您声明的对象类型


更新2:哦,还有一点不同。他用getter声明返回类的成员为private,而我使用public字段。因为它们是最终的,所以我不想用getter,但这是一个品味问题。

注意,我传入了文件,而不是硬连接它。更灵活。想想那个Person类,所有这些都不是你可以做一些事情来强制执行创建一个正确的名称的含义。电子邮件可以是空的吗?空白?imageLocation呢?如果这个人没有电子邮件或图像会发生什么?你将如何为只有一个名字的麦当娜或斯汀创建一个对象?有很多事情要考虑。啊,看起来好多了!我还是个新手(我相信您已经注意到)在OOP中,这对我来说是一个很好的学习步骤!谢谢!复制数组(可以更改),而不是包含的字符串。如果您希望避免调用方可以修改您的内部数组,那么这很有用。啊,您是对的。不过,我认为在这里使用copyAll没有多大意义。但我只是从我的答案中删除了关于copyAll的注释,这与问题无关。
public static class Person {
    public final String firstName, lastName, email, age, imgLoc;

    Person(String firstName, String lastName, String email, String age, String imgLoc) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.age = age;
        this.imgLoc = imgLoc;
    }
}

public List<Person> getData() throws FileNotFoundException {
    ArrayList<Person> list = new ArrayList<Person>();
    Scanner f = new Scanner(new File("Contacts.txt")).useDelimiter(",");
    while (f.hasNext()) {
        list.add(new Person(f.next(), f.next(), f.next(), f.next(), f.nextLine()));
    }
    f.close();
    return list;
}