【发布时间】:2020-04-07 19:46:24
【问题描述】:
你能告诉我为什么当我尝试从另一种方法启动时我看不到我的列表吗?以下方法:
public class CollectionsOperation {
private List<Client> bufferedReaderClientLIst = new ArrayList<Client>();
private List<Client> emptyBoxForCf = new ArrayList<Client>();
BufferedReader bf = null;
private static final String fileName = "Clients.txt";
public List<Client> bufferedReaderCollection() throws IOException {
String line;
bf = new BufferedReader(new InputStreamReader (new FileInputStream(fileName), "UTF-8"));
while((line = bf.readLine()) != null) {
String[] split = line.split(";");
String nameCompany = split[0].substring(2);
String adress = split[1];
String phoneNumber = split[2];
String emailAdress = split[3];
Client k = new Client(nameCompany, adress, phoneNumber, emailAdress);
bufferedReaderClientLIst.add(k);
}
System.out.println(bufferedReaderClientLIst);
return bufferedReaderClientLIst;
}
public void show() throws IOException {
CollectionsOperation k = new CollectionsOperation();
k.bufferedReaderCollection();
System.out.println(bufferedReaderClientLIst);
}
调用方法:
public static void main(String[] args) throws IOException {
CollectionsOperation k = new CollectionsOperation();
k.show();
}
这就是我得到的结果:
[ MarkCompany';Ilusiana';0982882902';mark@company.com, CorporationX';Berlin';93983';X@Corporation.com]
[]
为什么第二个列表是空的?方法bufferedReaderCollection() 返回一个结果,列表bufferedReaderClientLIst 可用于所有方法。怎么了?
【问题讨论】:
-
为什么
show()创建另一个CollectionsOperation对象?
标签: java arrays list methods return