首页 > 解决方案 > 在 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();
}

输出 1(错误)

输出 2(预期)

我只是使用 draw() 方法来更好地显示差异。问题是元素在范围之后消失了。或者我没有正确添加它们。它向我展示了我添加的最后一个元素,而不是我添加的每个元素。在这种情况下我错过了什么?

标签: javaarraylist

解决方案


看来您的问题出在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


推荐阅读