[英]How to iterate through ArrayList derived from object
Java和Android的新手...
我正在使用一个类来存储一组Google Map LatLng点以进行迭代。 这是我用来为对象分配Arraylist的类:
public class GeoPoints implements Iterable<String>, Iterator<String> {
private HashMap<String, Object> internalStorage = new HashMap<String, Object>();
private int count = 0;
public void add(String key, Object value) {
internalStorage.put(key, value);
}
public Object get(String key) {
return internalStorage.get(key);
}
@Override
public boolean hasNext() {
if (count < internalStorage.size()) {
return true;
}
return false;
}
@Override
public String next() {
if (count == internalStorage.size()) throw new IndexOutOfBoundsException();
count++;
return (String) internalStorage.keySet().toArray()[count - 1];
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public Iterator<String> iterator() {
return this;
}
}
我这样说:
geoPoints = new GeoPoints();
// Load GeoPoints ArrayList...
List<LatLng> points = new ArrayList<LatLng>();
points.add(new LatLng(45.778754, -124.811288));
points.add(new LatLng(28.781740, -124.350814));
points.add(new LatLng(48.712660, -60.982386));
points.add(new LatLng(20.354554, -58.794457));
geoPoints.add("US_MAP", points);
要访问该对象,我使用如下的for循环:
for (String key : geoPoints ) {
Object obj = geoPoints.get(key);
}
在这一点上,我可以调试并看到Object obj包含我用上面的四点创建的ArrayList。
为了我的一生,我无法弄清楚如何访问这些Arraylist值以迭代或遍历这些值。
我已经尝试将obj这样分配给arraylist迭代器:
ArrayList<?> myTestItr = geoPoints.get(key);
哪一个不起作用,我肯定有明显的原因... :)
我敢肯定对此有一个简单的答案(使用Iterator通配符等),但我很快就提出了。
任何帮助都将不胜感激!
提前致谢!
您是否尝试过如下声明地图:
private HashMap<String, List<LatLng>> internalStorage = new HashMap<String, List<LatLng>>();
这意味着您只能添加类型List<LatLng>
但可以轻松地对条目进行迭代。
您还必须编辑两种方法:
public void add(String key, List<LatLng>value) {
internalStorage.put(key, value);
}
public List<LatLng>get(String key) {
return internalStorage.get(key);
}
尝试以编程方式进行投射:
ArrayList<?> myTestItr = (ArrayList<?>) geoPoints.get(key);
您必须将Object类转换为ArrayList。
转换就像将对象转换为支持的Class Type一样。
如果不支持,它将抛出java.lang.ClassCastException
ArrayList<?> myTestItr = (ArrayList<?>) geoPoints.get(key);
这应该可以解决您的问题。
非常感谢,这里是解决方案和实现:
首先使用List LatLng类型的新类:
public class GeoPoints implements Iterable<String>, Iterator<String> {
private HashMap<String, List<LatLng>> internalStorage = new HashMap<String, List<LatLng>>();
private int count = 0;
public void add(String key, List<LatLng>value) {
internalStorage.put(key, value);
}
public List<LatLng> get(String key) {
return internalStorage.get(key);
}
@Override
public boolean hasNext() {
if (count < internalStorage.size()) {
return true;
}
return false;
}
@Override
public String next() {
if (count == internalStorage.size()) throw new IndexOutOfBoundsException();
count++;
return (String) internalStorage.keySet().toArray()[count - 1];
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public Iterator<String> iterator() {
return this;
}
}
添加到对象:
geoPoints = new GeoPoints();
// Load GeoPoints ArrayList...
List<LatLng> points = new ArrayList<LatLng>();
points.add(new LatLng(45.778754, -124.811288));
points.add(new LatLng(28.781740, -124.350814));
points.add(new LatLng(48.712660, -60.982386));
points.add(new LatLng(20.354554, -58.794457));
geoPoints.add("US_MAP", points);
要遍历集合:
for (String key : geoPoints) {
List<LatLng> geoPointsXY = geoPoints.get(key);
Iterator geoPtsItr = geoPointsXY.iterator();
while (geoPtsItr.hasNext()){
LatLng oNxtLatLng = (LatLng) geoPtsItr.next();
double yt = oNxtLatLng.latitude;
double nt = oNxtLatLng.longitude;
// Verify Coords here:...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.