我有一个名为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);
}
答案 0 :(得分:0)
// 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();