我正在将 Android 应用程序 (java) 移植到 ios 应用程序。其中一部分是实时显示图表。
我使用此代码将值插入到数组中的索引 0:
public double[] points;
.....
//Clear array value at first index
System.arraycopy(points, 0, points, 1, points.length - 1);
//Add new value to first index
points[0] = sample;
对此有一些疑问,但对我的案例没有用。
我可以使用 NSMutableArray 和 NSNumber 来获取结果,但它需要更多的代码和 CPU
最佳答案
在 C 语言中,一开始会这样做:
size_t n_points = 512;
double *points = calloc(512, sizeof(double));
<小时/>
那么逻辑是:
memmove(points + 1, points, n_points - 1);
*points = sample;
<小时/>
虽然更好的是使用环形缓冲区 - 因此不必在缓冲区周围移动值,只需移动被视为开头的索引即可:
size_t n_points = 512;
double *points = calloc(512, sizeof(double));
ssize_t beginning = 0;
<小时/>
if (--beginning < 0) {
beginning += n_points;
}
points[beginning] = sample;
然后在绘图代码中:
ssize_t idx, i;
for (idx = beginning, i = 0;
i < n_points;
i ++, idx = (idx + 1) % n_points)
{
// i runs from 0 ... n_points - 1
set_pixel(i, points[idx], black);
}
关于java - C、C++ 或 Objective-C 中的 System.arraycopy() 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29186593/