我有一个ArrayList()
,它存储了一些实时生成的数据。我想得到ArrayList
我试过这个
ArrayList<Long> difference = new ArrayList<>();
difference.add(durationInMs);
//tried to store
for (int d = 0; d < difference.size()-1; d++){
time1 = difference.get(d);
time2 = difference.get(d + 1);
timeDifference = time2 - time1;
}
此:----------------
ArrayList<Long> difference = new ArrayList<>();
difference.add(durationInMs);
ArrayList<Long> tempArray = new ArrayList<>();
tempArray.add(0, difference.get(0));
for (int d = 0; d < difference.size()-1; d++){
timeDifference = tempArray.get(d+1) - difference.get(d);
}
Log.d("time", String.valueOf(timeDifference));
但是他们似乎没有给我两个连续元素之间的减去值。可能问题是值是实时生成的,这在减去时,尚未生成下一个值。 我该怎么做?
更新:方法(如果有帮助)
@Override
public void onPreviewFrame(long startTime, int ySum, int uSum, int vSum, long endTime) {
begin = startTime;
img_Y_Avg = ySum;
img_U_Avg = uSum;
img_V_Avg = vSum;
end = endTime;
//set value of Y on the text view
TextView valueOfY = (TextView)findViewById(R.id.valueY);
//valueY = img_Y_Avg;
valueOfY.setText(Double.toString(img_Y_Avg));
//set value of U on the text view
TextView valueOfU = (TextView)findViewById(R.id.valueU);
// valueU = img_U_Avg;
valueOfU.setText(Double.toString(img_U_Avg));
//set value of V on the text view
TextView valueOfV = (TextView)findViewById(R.id.valueV);
//valueV = img_V_Avg;
valueOfV.setText(Double.toString(img_V_Avg));
//start time in milliseconds
long StartDurationInMs = TimeUnit.MILLISECONDS.convert(begin, TimeUnit.MILLISECONDS);
ArrayList<Long> startOfTime = new ArrayList<>();
startOfTime.add(StartDurationInMs);
Log.d("time", "start: "+String.valueOf(startOfTime));
//store value to array list
ArrayList<Integer> yAverage = new ArrayList<>();
yAverage.add(img_Y_Avg);
//Log.d("MyEntryData", String.valueOf(yAverage));
//store u values to array
ArrayList<Integer> uAverage = new ArrayList<>();
uAverage.add(img_U_Avg);
//Log.d("MyEntryData", String.valueOf(uAverage));
//store u values to array
ArrayList<Integer> vAverage = new ArrayList<>();
vAverage.add(img_V_Avg);
//Log.d("MyEntryData", String.valueOf(vAverage));
//time between frames
//end time in milliseconds
long EndDurationInMs = TimeUnit.MILLISECONDS.convert(end, TimeUnit.MILLISECONDS);
ArrayList<Long> endOfTime = new ArrayList<>();
endOfTime.add(EndDurationInMs);
Log.d("time", "end: "+String.valueOf(endOfTime));
ArrayList<Long> getValues = new ArrayList<>();
for(int i = 0; i < yAverage.size(); i++) {
getValues.add(startOfTime.get(i));
getValues.add((long)(yAverage.get(i)));
getValues.add((long)(uAverage.get(i)));
getValues.add((long)(vAverage.get(i)));
}
String filename = new SimpleDateFormat("yyyyMMddHHmm'.csv'").format(new Date());
File directoryDownload = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File logDir = new File (directoryDownload, "bpReader"); //Creates a new folder in DOWNLOAD directory
logDir.mkdirs();
File file = new File(logDir, filename);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file, true);
//outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
for (int i = 0; i < yAverage.size(); i += 4) {
outputStream.write((getValues.get(i) + ",").getBytes());
outputStream.write((getValues.get(i + 1) + ",").getBytes());
outputStream.write((getValues.get(i + 2) + ",").getBytes());
outputStream.write((getValues.get(i + 3) + "\n").getBytes());
}
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.d("MyEntryData", String.valueOf(getValues));
handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
readingRemaining = readingRemaining -1;
if (readingRemaining > 0){
plotGraph(img_Y_Avg);
}
}
};
handler.postDelayed(runnable, 100);
//Log.d("MyEntryData", String.valueOf(img_Y_Avg +" "+ img_U_Avg+" "+img_V_Avg));
}
答案 0 :(得分:0)
尝试long delta = difference.get(d + 1) - difference.get(d);
ArrayList<Long> difference = new ArrayList<>();
for (int i = 0; i<10; i++)
difference.add((long)(Math.random() * 1000));
for (int d = 0; d < difference.size()-1; d++){
Log.i("diff", String.format(
"element %d,%d:%d - %d = %d",
d+1, d, difference.get(d+1), difference.get(d),
difference.get(d+1) - difference.get(d)));
}
Log.i(TAG, "hello");
输出:
... I/diff: element 1,0:197 - 425 = -228
... I/diff: element 2,1:389 - 197 = 192
... I/diff: element 3,2:947 - 389 = 558
... I/diff: element 4,3:510 - 947 = -437
... I/diff: element 5,4:399 - 510 = -111
... I/diff: element 6,5:971 - 399 = 572
... I/diff: element 7,6:537 - 971 = -434
... I/diff: element 8,7:326 - 537 = -211
... I/diff: element 9,8:897 - 326 = 571
答案 1 :(得分:0)
如果您担心在处理数组时实时修改数组,那么
建议1
在生成新数据点时,您是否可以查看先前的数据点并在运行中计算delta,然后将delta()添加到另一个数组?
long lastDatum = null;
ArrayList<long> diffArray = new ArrayList<>();
void recordDataPoint(long datum)
{
if (lastDatum != null) {
diffArray.add(datum - lastDatum);
}
lastDatum = datum;
// ... do whatever else you need to do with datum....
}
建议2
如果阵列稳定,那么我认为你只需要这样做:
// existing data is in durationInMs[]
ArrayList<Long> difference = new ArrayList<>();
for (int d = 0; d < durationInMs.size()-1; d++){
time1 = durationInMs.get(d);
time2 = durationInMs.get(d + 1);
timeDifference = time2 - time1;
difference.add(timeDifference);
}