除了我的评论,这里还有 3 种你不能做到的方法
读入单个数组
int size = 2;
// first allocate some memory for each of your arrays
int[] transID = new int[size];
int[] transCode = new int[size];
String[] time = new String[size];
double[] trasAmount = new double[size];
Scanner reader = new Scanner(new File("sales.txt"));
// keep track of how many elements you have read
int i = 0;
// start reading and continue untill there is no more left to read
while(reader.hasNext()) {
// since array size is fixed and you don't know how many line your file will have
// you have to reallocate your arrays when they have reached their maximum capacity
if(i == size) {
// increase capacity by 5
size += 5;
// reallocate temp arrays
int[] tmp1 = new int[size];
int[] tmp2 = new int[size];
String[] tmp3 = new String[size];
double[] tmp4 = new double[size];
// copy content to new allocated memory
System.arraycopy(transID, 0, tmp1, 0, transID.length);
System.arraycopy(transCode, 0, tmp2, 0, transCode.length);
System.arraycopy(time, 0, tmp3, 0, time.length);
System.arraycopy(trasAmount, 0, tmp4, 0, trasAmount.length);
// reference to the new memory by your old old arrays
transID = tmp1;
transCode = tmp2;
time = tmp3;
trasAmount = tmp4;
}
// read
transID[i] = Integer.parseInt(reader.next());
transCode[i] = Integer.parseInt(reader.next());
time[i] = reader.next();
trasAmount[i] = Double.parseDouble(reader.next());
// increment for next line
i++;
}
reader.close();
for(int j = 0; j < i; j++) {
System.out.println("" + j + ": " + transIDList.get(j) + ", " + transCodeList.get(j) + ", " + timeList.get(j) + ", " + trasAmountList.get(j));
}
如您所见,这是很多代码。
最好使用列表,从而摆脱重新分配和复制的开销(至少在您自己的代码中)
读入单个列表
// instanciate your lists
List<Integer> transIDList = new ArrayList<>();
List<Integer> transCodeList = new ArrayList<>();
List<String> timeList = new ArrayList<>();
List<Double> trasAmountList = new ArrayList<>();
reader = new Scanner(new File("sales.txt"));
int i = 0;
while(reader.hasNext()) {
// read
transIDList.add(Integer.parseInt(reader.next()));
transCodeList.add(Integer.parseInt(reader.next()));
timeList.add(reader.next());
trasAmountList.add(Double.parseDouble(reader.next()));
i++;
}
reader.close();
for(int j = 0; j < i; j++) {
System.out.println("" + j + ": " + transIDList.get(j) + ", " + transCodeList.get(j) + ", " + timeList.get(j) + ", " + trasAmountList.get(j));
}
你看到这里的代码有多小了吗?但是还是可以好起来的...
文件中的一行sales.txt
似乎构成了某个实体的数据元素,为什么不把它们放在一个对象中呢?为此,您可能会写一个class
named Trans
,有些人会这样想:
class Trans {
public int transID;
public int transCode;
public String time;
public double trasAmount;
@Override
public String toString() {
return transID + ", " + transCode + ", " + time + ", " + trasAmount;
}
}
然后你可以使用这个类来保存你从文件中读取的数据,并将该类的每个对象放在一个列表中。
读入对象列表
reader = new Scanner(new File("sales.txt"));
List<Trans> transList = new ArrayList<>();
int i = 0;
while(reader.hasNext()) {
Trans trans = new Trans();
trans.transID = Integer.parseInt(reader.next());
trans.transCode = Integer.parseInt(reader.next());
trans.time = reader.next();
trans.trasAmount = Double.parseDouble(reader.next());
transList.add(trans);
i++;
}
reader.close();
for(Trans trans : transList) {
System.out.println("" + i++ + ": " + trans);
}
所有 3 种方法的输出
0: 350279, 1, 11:54, 107.15
1: 350280, 3, 11:55, 81.27
2: 350281, 2, 11:57, 82.11
3: 350282, 0, 11:58, 92.43
4: 350283, 3, 11:59, 86.11