我的积分等级是不可变的。当我最初输入构造函数时,它应该被复制到 cloneList 对象中。如果通过构造函数更改数组中的几个索引,这将允许它保持以前的方式。我已经尝试了几乎所有可能的组合,但仍然遇到了麻烦。我希望 cloneList 是原始 Point[] points 数组的副本,因此如果 points 数组发生更改,则 cloneList 不会。
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class Polygon {
private double xSum = 0;
private double ySum = 0;
private Point[] points;
private Point[] cloneList;
private Point a;
public Polygon(Point[] points) {
this.points = points;
cloneList = new Point[points.length];
for (int i = 0; i < cloneList.length; i++) {
cloneList[i] = points[i];
}
for (int i = 0; i < cloneList.length; i++)
System.out.println(cloneList[i]);
for (int i = 0; i < points.length; i++){
cloneList[i] = points[i];
// System.out.print(cloneList[i].getX());
// System.out.print(cloneList[i].getY());
// System.out.println();
}
public Point getVertexAverage() {
double xSum = 0;
double ySum = 0;
for (int index = 0; index < cloneList.length; index++) {
xSum = xSum + cloneList[index].getX();
ySum = ySum + cloneList[index].getY();
}
return new Point(xSum / getNumSides(), ySum / getNumSides());
}
public int getNumberSides() {
return cloneList.length;
}
}