0

你能告诉我如何将一个overlayItem对象添加到一个数组中吗?我试过这样:

    GeoPoint point = new GeoPoint((int)(Double.parseDouble(arrCoordonate[1])),(int)(Double.parseDouble(arrCoordonate[0])));
    OverlayItem overlayItem = new OverlayItem(point, Double.parseDouble(arrCoordonate[1]) + "", Double.parseDouble(arrCoordonate[0]) +"");
    List<OverlayItem> arrItem[] = overlayItem;

但我得到一个错误:

类型不匹配:无法从 OverlayItem 转换为 List[]

4

2 回答 2

1

You are mixing arrays and lists. If you don't have to use an array, using a list is easier, in which case you probably meant to write:

List<OverlayItem> itemList = new ArrayList<OverlayItem> (); // create an empty list
itemList.add(overlayItem); // add you item to the list

If you want to use an array you would write it like this - but you need to manage the size of the array yourself (which is why using a list as above is easier):

OverlayItem[] itemArray = new OverlayItem[10]; //if you only need to insert 10 items
itemArray[0] = overlayItem;
于 2012-05-28T12:08:08.577 回答
0

This is right because look at the decleartion: OverlayItem overlayItem and List<OverlayItem> arrItem[].
So if you arrItem[] = overlayItem you implicit claim List<OverlayItem> = OverlayItem. :)
(I'm not sure but I think it's even "worse" with the [] at the end of variable's name. I think the brackets are reserved for arrays thus List<OverlayItem> arrItem[] results in an Array of Lists o.O)

You want to create a new List of OverlayItems and thenn add the item to this list. I'm not sure if you can instantiate List directly so I use ArrayList:

GeoPoint point = new GeoPoint((int)(Double.parseDouble(arrCoordonate[1])),(int)(Double.parseDouble(arrCoordonate[0])));
    OverlayItem overlayItem = new OverlayItem(point, Double.parseDouble(arrCoordonate[1]) + "", Double.parseDouble(arrCoordonate[0]) +"");
    ArrayList<OverlayItem> arrItem = new ArrayList<OverlayItem>();
    arrItem.add(overlayItem);
于 2012-05-28T12:09:38.667 回答