2011-09-28 139 views
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是一个猜测。我不知道我是否在朝着正确的方向前进。

回答

10

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

我更喜欢一个对象,它将第一个,最后一个,电子邮件,年龄和图像封装到一个Person类中,并返回一个List。

Java的一种面向对象的语言。如果你停止思考像Strings,ints和数组这样的基本元素,并开始用对象来思考,你会做得更好。只要有可能,就把那些可以聚集在一起的东西封装成一个单一的物体。

以下是我会写这个方法:

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

更少的代码,并且更容易理解。

+0

请注意,我通过了文件而不是硬连线。更灵活。想想那个Person类以及你能做的所有事情来实现创建一个正确的方法。电子邮件可以为空吗?空白? imageLocation怎么样?如果Person没有电子邮件或图像,会发生什么?你将如何为麦当娜或斯汀创造一个只有一个名字的物体?很多想法。 – duffymo

+0

啊,甜美,看起来好多了!在OOP中,我还是相当新的(因为我敢肯定你注意到了),所以这对我来说是一个很好的学习步骤!谢谢! – 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,既然他们是最终的,我不会为吸气者而烦恼,但这是一个品味问题。

+0

copyOf复制数组(可变),而不是包含的字符串。如果你想避免调用者可以修改你的内部数组,这很有用。 –

+0

啊,你说得对。不过,我认为copyAll的使用在这里没有多大意义。但我只是从我的答案中删除了有关copyAll的评论,无论如何它都是无关紧要的。 – njlarsson