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

3

如果我想要从一个方法中返回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;
}

使用 ArrayList 只是一个猜测。我不确定我是否朝着正确的方向前进。

3个回答

10

我认为这是一个糟糕的想法。

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

Java是一种面向对象的语言。如果您停止使用像String、int和数组这样的基本类型,开始使用对象思考,就会做得更好。在可以的情况下,将应该放在一起的东西封装成单个对象。

以下是我写该方法的方式:

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;
}

代码更少,更易于理解。


请注意,我传递了文件而不是硬编码它。更加灵活。考虑一下Person类以及您可以执行的所有操作,以强制创建正确对象的含义。电子邮件可以为空吗?空白?那么imageLocation呢?如果Person没有电子邮件或图像会发生什么?如何为只有一个名字的Madonna或Sting创建对象?有很多要考虑的事情。 - duffymo
啊,太好了,看起来好多了!我还是相对新手(我相信你已经注意到了),所以这对我来说是一个很好的学习步骤!谢谢! - user968366

0

更清晰的实现(虽然缺乏错误检查...)

public class Person {
    private final String fName;
    private final String lName;
    private final String email;
    private final String age;
    private final String imgLoc;

    public Person(String fName, String lName, String email, String age,
            String imgLoc) {
        super();
        this.fName = fName;
        this.lName = lName;
        this.email = email;
        this.age = age;
        this.imgLoc = imgLoc;
    }

    /* ...Getters here... */
}

public Object getData()throws FileNotFoundException{
    ArrayList<Person> out = new ArrayList<Person>();

    Scanner f = new Scanner(new File("Contacts.txt")).useDelimiter(",");
    while (f.hasNext()){
        out.add(new Person(
                f.next(),
                f.next(),
                f.next(),
                f.next(),
                f.next() ));
    }
    f.close();

    return out;
}

0

这是我会做的:

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;
}

更新:当我写这篇文章时,claymore1977发布了一个几乎相同的答案。唯一值得注意的区别是,我声明了方法的类型为List,这实际上就是它的类型,而他保留了你声明的Object类型。

更新2:哦,还有一个区别。他将返回类的成员声明为私有,并使用getter方法,而我使用公共字段。由于它们是final的,我不会费心去写getter方法,但这只是个人口味问题。


copyOf方法复制的是数组(可变类型),而不是其中包含的字符串。如果您想避免调用者修改您的内部数组,这将非常有用。 - Paŭlo Ebermann
啊,你说得对。不过我觉得在这里使用copyAll没有太多意义。但是我已经从我的答案中删除了关于copyAll的评论,因为它无关紧要。 - njlarsson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接