0

我已经多次研究过这段代码,但似乎无法理解它的行为。

我在 for 循环中添加了多个实例,同时还创建了另一个,它是添加到数组中的所有实例的合并(但是,这个合并Area的对象从未添加到)。ArrayListAreaAreaArrayList

当试图从另一个类中检索第一个项目时,它给了我从未添加过ArrayList的合并边界?!?!Area

import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;


public class CorruptArray {
public ArrayList<Area> arrayList = new ArrayList<Area>();

public CorruptArray(){
        double x = 100.0;
        double y = 200.0;
        double width = 100.0;
        double height = 200.0;
        Area map = null;

        int i = 5;
        for(int j =0 ; j<i; j++){
            arrayList.add(new Area(new Rectangle2D.Double(x,y,width,height)));

            x+=10;
            y+=10;
            width+=10;
            height+=10;
        }

        int k = 0;
        for(Area area : arrayList){
            System.out.println("INITIAL BOUNDS ON ENTRY:  " + area.getBounds().toString());
            if(k == 0){
                map = area;
                k++;

            }else{
                map.add(area);

            }
            System.out.println("OVERALL JOINED AREA - map   " + map.getBounds().toString() + "\n");
        }

        TestArray test = new TestArray(this);
    }

private static class TestArray{
    TestArray(CorruptArray ca){
        System.out.println("\n\n");
        for(Area area : ca.arrayList){
            System.out.println("RETRIVAL OF BOUNDS FROM TestArray:  " + area.getBounds().toString());
        }
    }
}



public static void main(String args[]){
    new CorruptArray();
}

运行程序时,这是我得到的输出:

INITIAL BOUNDS ON ENTRY:  java.awt.Rectangle[x=100,y=200,width=100,height=200]
OVERALL JOINED AREA - map java.awt.Rectangle[x=100,y=200,width=100,height=200]

INITIAL BOUNDS ON ENTRY:  java.awt.Rectangle[x=110,y=210,width=110,height=210]
OVERALL JOINED AREA - map java.awt.Rectangle[x=100,y=200,width=120,height=220]

INITIAL BOUNDS ON ENTRY:  java.awt.Rectangle[x=120,y=220,width=120,height=220]
OVERALL JOINED AREA - map java.awt.Rectangle[x=100,y=200,width=140,height=240]

INITIAL BOUNDS ON ENTRY:  java.awt.Rectangle[x=130,y=230,width=130,height=230]
OVERALL JOINED AREA - map java.awt.Rectangle[x=100,y=200,width=160,height=260]

INITIAL BOUNDS ON ENTRY:  java.awt.Rectangle[x=140,y=240,width=140,height=240]
OVERALL JOINED AREA - map java.awt.Rectangle[x=100,y=200,width=180,height=280]




RETRIVAL OF BOUNDS FROM TestArray:  java.awt.Rectangle[x=100,y=200,width=180,height=280]
RETRIVAL OF BOUNDS FROM TestArray:  java.awt.Rectangle[x=110,y=210,width=110,height=210]
RETRIVAL OF BOUNDS FROM TestArray:  java.awt.Rectangle[x=120,y=220,width=120,height=220]
RETRIVAL OF BOUNDS FROM TestArray:  java.awt.Rectangle[x=130,y=230,width=130,height=230]
RETRIVAL OF BOUNDS FROM TestArray:  java.awt.Rectangle[x=140,y=240,width=140,height=240]
4

1 回答 1

3

您不添加合并区域,而是从数组列表中获取第一个区域并对其进行修改。它恰好发生在这里:

if (k == 0) {
   map = area;

稍后您将列表中的其他区域添加到地图 - 这是列表中的第一个区域。您可能希望根据列表中的第一个区域创建一个新区域:

if (k == 0) {
   map = new Area(area);
于 2013-07-06T21:55:02.337 回答