English 中文(简体)
我如何能够从某种方法中回归多个阵列?
原标题:How can I return multiple arrays from a method?
  • 时间:2011-09-28 11:37:14
  •  标签:
  • java
  • arrays

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?

这是我迄今为止所做的。

 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为gues。 我不敢肯定,我在那里领导正确的方向。

问题回答

我认为这是一个可怕的想法。

我更喜欢一字标,一字先封,最后是电子邮件、年龄和形象,然后回到其中。

Java是一种面向目标的语言。 如果你停止在“强权”、“ts”和“阵列”等原始问题上进行思考,并开始思考物体,你就会做得更好。 只要你能够,就把意图合在一起的东西归入单一物体。

这里我是这样写的:

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

在此,我要做的是:

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: 克莱莫尔·1977年作了几乎完全相同的回答,因为我是这样写的。 唯一显著的区别是,我宣布了名单所用方法的类型,这是实际的,他保留了你宣布的反对类型。

<>Update 2 Oh是另一个区别。 他宣布返回类别的成员是私人的,有游民,而我则使用公共领域。 既然是最终的,我不会与登机站在一起,而是说出一个tas。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签