我有一个名为x=points
的数组。在此数组中有十个坐标(无论如何,它们的x
部分)。我想将10 * c
的值添加到数组中的每个点。另外,这是在Java中。
我该怎么办?
for (int c = 0; c <= 9; c++) {
for(int k = 0; k <= 9; k++) {
if (k%2 == 0) {
xpoints[k] = (int) (20*Math.cos((k*3.14/5)+3*3.14/2)) +60;
ypoints[k] = (int) (20*Math.sin((k*3.14/5)+3*3.14/2)) +60;
} else if (k%2 == 1) {
xpoints[k] = (int) (8*Math.cos((k*3.14/5)+3*3.14/2)) +60;
ypoints[k] = (int) (8*Math.sin((k*3.14/5)+3*3.14/2)) +60;
}
}
// This is where I am having trouble
xpoints[] = xpoints + 5*c;
g.fillPolygon(xpoints, ypoints, npoints);
}
最佳答案
// This is where I am having trouble
xpoints[] = xpoints + 5*c;
要更新数组中索引
i
处的值,请按其索引访问它并重新分配更新后的值:xpoints[i] = xpoints[i] + 10 * c;
或更短:
xpoints[i] += 10 * c;
要遍历整个数组并更新每个单个值:
for (int i = 0; i < xpoints.length; i++) {
xpoints[i] += 10 * c;
}
或使用Java 8流:
xpoints = Arrays.stream(xpoints).map(xpoint -> xpoint + 10 * c).toArray();