开发者

How can I return multiple arrays from a method?

Say that I wanted to return 6 arrays from one method to another (in another class). What is the best way of doing this and why?

This is what I have so far.

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

Using an ArrayList was a guess. I'm not sure if I'm headed in the right direction there.


I think this is a terrible idea.

I'd prefer one object that encapsulates first, last, email, age, and image into a Person class and return a List of those.

Java's an object-oriented language. You'll do better if you stop thinking in terms of primitives like Strings, ints, and arrays and start thinking in terms of objects. Encapsulate things that are meant to be together into a single object whenever you can.

Here's how I'd write that method:

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

Less code, and easier to understand.


Cleaner implementation (though lacking in error checking...)

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


Here's what I would do:

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

Update: An almost identical answer was posted by claymore1977 as I was writing this. The only notable difference is that I declared the type of the method to be List, which is what it actually is, while he retained your declared type of Object.

Update 2: Oh, one more difference. He declared members of the return class private, with getters, while I used public fields. Since they are final, I would not bother with getters, but it's a matter of taste.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜