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