java—向数组的内容添加值

xbp102n0  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(315)

我有一个数组叫做 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);
}
pcww981p

pcww981p1#

// 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();

相关问题