假设我想将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
是猜测。我不确定我是否朝着正确的方向前进。
答案 0 :(得分:10)
我认为这是一个糟糕的主意。
我更喜欢一个将first,last,email,age和image封装到Person类中的对象,并返回一个List。
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;
}
代码少,易于理解。
答案 1 :(得分: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;
}
答案 2 :(得分: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 :哦,还有一个区别。当我使用公共领域时,他宣称返回班的成员是私人的,有吸气剂。由于它们是最终的,我不会打扰吸气剂,但这是一个品味问题。