java - 在 Java 中向 ArrayList 添加和到达元素的问题
问题描述
我正在研究 2D 图形优化拟合项目。我用 C++ 编码并更改为 Java,因此我知道该算法有效。但是我在到达循环范围之外的 ArrayList 元素或更可能添加到 ArrayList 时遇到问题。
我可以在这个范围内找到我想要的元素,但是在这个范围之外,一些元素会丢失。我知道这无关紧要,并且可能发生了超出我注意力的事情。
三角班:
public class Triangle implements Shape, Cloneable
{
private double length; // size of equaliteral triangle's each edge.
private double x,y;
private boolean rotate; // Flag for rotate by 90 degress around pos(x,y)
private boolean fill; // Flag for fill
private static double total_area = 0;
private static double total_perim = 0;
private int[] xPoints;
private int[] yPoints;
.
...
}
定义:
Triangle t2 = (Triangle)small;
Triangle t = (Triangle)t2.clone();
List<Shape> shape = new ArrayList<Shape>();
在下面的代码中,我一将它添加到列表中就绘制它。方法 draw() 在这种情况下无关紧要,它只使用 x 和 y 等字段。
代码 1:
// (a,width-a*sqrt(3)) init for fill inside without rotating
for(y = r1.getHeight()-tri_height;y>=0;y-=tri_height)
{
x=t.getLength()/2.0;
while(x+t.getLength()/2.0<=r1.getWidth())
{
t.setPosition(x+_x,y+_y);
shape.add((Triangle)t.clone());
shape.get(size).draw(g); // check this line.
++size;
x+=t.getLength();
}
}
在同一段代码中,我只在插入完成后绘制/打印它们。
代码 2:
// (a,width-a*sqrt(3)) init for fill inside without rotating
for(y = r1.getHeight()-tri_height;y>=0;y-=tri_height)
{
x=t.getLength()/2.0;
while(x+t.getLength()/2.0<=r1.getWidth())
{
t.setPosition(x+_x,y+_y);
shape.add((Triangle)t.clone());
x+=t.getLength();
}
}
for(Shape s:shape)
s.draw(g);
克隆()方法:
@Override
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
我只是使用 draw() 方法来更好地显示差异。问题是元素在范围之后消失了。或者我没有正确添加它们。它向我展示了我添加的最后一个元素,而不是我添加的每个元素。在这种情况下我错过了什么?
解决方案
看来您的问题出在Triangle.clone()
方法上。Triangle
您在、int[] xPoints
或中有引用int[] yPoints
。
默认实现Object.clone()
仅适用于简单类型,但不适用于引用。
Triangle t = (Triangle)t2.clone();
t.xPoints == t2.xPoints; // this is same array (not a copy)
你所有的矩形都画在同一个地方。
怎么解决
不要使用clone()
方法。一般来说,它已经过时了。您必须创建C++ copy constructor
对象并手动创建对象的副本。
public class Triangle implements Shape {
private static double total_area = 0;
private static double total_perim = 0;
private double length;
private double x,y;
private boolean rotate;
private boolean fill;
private int[] xPoints;
private int[] yPoints;
public Triangle(Triangle triangle) {
this.length = triangle.length;
this.x = triangle.x;
this.y = triangle.y;
this.rotate = triangle.rotate;
this.fill = triangle.fill;
this.xPoints = xPoints != null ? Arrays.copyOf(xPoints, xPoints.length) : null;
this.yPoints = yPoints != null ? Arrays.copyOf(yPoints, yPoints.length) : null;
}
}
附言
int[] xPoints
: xPoints
- 不是数组,这是对数组的引用int
。
int[] xPointsCopy = xPoints
: - 这是对同一数组的xPointsCopy
另一个引用。int
推荐阅读
- xml - Create custom XML from Access VBA
- java - 与docker上的mysql服务器通信?
- c - Inputing a value and instead of keeping that information in a array, it changes another var
- html - 导航栏显示不正确
- java - Spring ConstraintValidator 设置自定义代码/拒绝值
- vue.js - 如何结合 v-for 和 v-model 来编辑对象列表?
- android-studio - 找不到符号变量 imageView2
- r - Igraph 基于中心性分数识别节点
- javascript - 为什么我的 redux 商店没有获得 firebase 电子邮件属性?反应
- javascript - 如何在自动添加的 div 中添加不同的数字?