我的代码的问题是,当我generateCSV()
在下面调用时,ArrayList 的输出不正确。它正在用仅最后一个要实例化的实例替换所有实例,即最后一个实例正在替换所有先前的事件。
我的基类PausePred
接受一个字符串和一个 ArrayList
public class PausePred {
private String predString; // the keys preceding the pause
private ArrayList<KSE> kseArr; // the arraylist of KSEs comprising the PausePred
public PausePred(String predStr) {
this.predString = predStr;
kseArr = new ArrayList<KSE>();
}
public void setKseArr(ArrayList<KSE> kseArr) {
this.kseArr.addAll(kseArr);
}
在PausePred
中,我有一个静态方法来创建PausePreds
.
public static Collection<PausePred> parseKseArray(KSE[] kArr) {
Collection<PausePred> pausePredArray = new ArrayList<PausePred>();
String pausePredStr = ""; // to store incrementally appended events preceding pause
int pauseDur = 0; // to store pause duration
boolean startPosSet = false; // checks if startPos has been set
ArrayList<KSE> pausePredKseArr = new ArrayList<KSE>();;
for (int kseArrIdx = 0; kseArrIdx < kArr.length; kseArrIdx++) {
if (kArr[kseArrIdx].isKeyPress()) { // only down-keys
// if start positio"n has not been set, set it, and update flag
// set time stamp, as well
if (startPosSet == false) {
startPosSet = true;
// get first char of PredStr by going back one index
if (kseArrIdx > 0) {
pausePredStr = VisualCharStream.vkCodetoString(kArr[kseArrIdx-1].getKeyCode());
pausePredKseArr.add(kArr[kseArrIdx-1]);
}
}
if (kArr[kseArrIdx].getM_pauseMs() < PauseBursts.PAUSE) { // is not a pause
//append vkCode (to string) to pausePred
pausePredStr += VisualCharStream.vkCodetoString(kArr[kseArrIdx].getKeyCode());
pausePredKseArr.add(kArr[kseArrIdx]);
}
else { // is a pause
//start incrementing pause duration, until a non-pause is reached
while (kArr[kseArrIdx].getM_pauseMs() >= PauseBursts.PAUSE) {
pauseDur = (int) kArr[kseArrIdx].getM_pauseMs();
kseArrIdx++;
}
//add to pausePred array
PausePred pp = new PausePred(pausePredStr);
pp.setKseArr(pausePredKseArr);
pausePredArray.add(pp);
//reset variables
pausePredStr = "";
startPosSet = false;
pausePredKseArr.clear();
//need to take one step back from above while loop
kseArrIdx--;
}
} // close outer if loop
} // close for loop
return pausePredArray;
} // close parseKseArray()
当我在下面调用此方法时,第一部分给出了每个实例,但第二部分只给出了数组中最后一个实例的 kseArr。
public static void generateCSV(String fileName,ArrayList<PausePred> pausePredArr) {
for (PausePred pp : pausePredArr)
System.out.println(pp.getPredString()+"\t"+pp.kseArr.get(pp.kseArr.size()-1).getKeyCode());
}
这是调用上面 parse 方法的提取方法。我不确定它是否与这个问题密切相关。
public class ExtractPausePred implements ExtractionModule {
private static ArrayList<PausePred> pausePredArray = new ArrayList<PausePred>();
@Override
public void extract(DataNode data) {
for (Answer a : data) {
//create KSE array
KSE[] kseArr = parseSessionToKSE(a.getKeyStrokes());
//from above KSE array, extract Pause Predecessors
pausePredArray.addAll(PausePred.parseKseArray(kseArr));
PausePred.generateCSV("testing123",pausePredArray);
}
return null;
}
}