2014-11-03 2 views
0

나는 안드로이드 지오 펜싱에 대한 다음과 같은 클래스가 :안드로이드 : 2D ArrayList를 반환 indexoutofbounds는

public class gpsLocationChecker { 

    List<ArrayList<location>> locations = new ArrayList<ArrayList<location>>(); 

    //public location locations[][] = new location[8][500]; 

    InputStream in; 
    String[] oblastiGPSArray; 

    public gpsLocationChecker(InputStream in, String[] oblastiGPSArray){ 
     this.in = in; 
     this.oblastiGPSArray = oblastiGPSArray; 
     try { 
      loadParser(); 
     }catch(Exception e){} 
    } 

    public String findPoint(Double Long, Double Lat){ 
     for(int i = 0; i <= locations.size(); i++){ 
      if(PointIsInRegion(Long, Lat, locations.get(i))) 
      { 
       return(oblastiGPSArray[i]); 
      } 
     } 
     return "abc"; 
    } 
    private boolean PointIsInRegion(double x, double y, ArrayList<location> oblast) 
    { 
     int crossings = 0; 

     location point = new location (x, y); 
     int count = oblast.size(); 
     // for each edge 
     for (int i=0; i < count; i++) 
     { 
      location a = oblast.get(i); 
      int j = i + 1; 
      if (j >= count) 
      { 
       j = 0; 
      } 
      location b = oblast.get(j); 
      if (RayCrossesSegment(point, a, b)) 
      { 
       crossings++; 
      } 
     } 
     // odd number of crossings? 
     return (crossings % 2 == 1); 
    } 
    boolean RayCrossesSegment(location point, location a, location b) 
    { 
     double px = point.Long; 
     double py = point.Lat; 
     double ax = a.Long; 
     double ay = a.Lat; 
     double bx = b.Long; 
     double by = b.Lat; 
     if (ay > by) 
     { 
      ax = b.Long; 
      ay = b.Lat; 
      bx = a.Long; 
      by = a.Lat; 
     } 
     // alter longitude to cater for 180 degree crossings 
     if (px < 0) { px += 360; }; 
     if (ax < 0) { ax += 360; }; 
     if (bx < 0) { bx += 360; }; 

     if (py == ay || py == by) py += 0.00000001; 
     if ((py > by || py < ay) || (px > Math.max(ax, bx))) return false; 
     if (px < Math.min(ax, bx)) return true; 

     double red = (ax != bx) ? ((by - ay)/(bx - ax)) : Float.MAX_VALUE; 
     double blue = (ax != px) ? ((py - ay)/(px - ax)) : Float.MAX_VALUE; 
     return (blue >= red); 
    } 
    private void loadParser() throws XmlPullParserException, IOException { 
     try { 
      XmlPullParser parser = Xml.newPullParser(); 
      parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); 
      parser.setInput(in, null); 
      loadData(parser); 
     } finally { 
      in.close(); 
     } 
    } 
    private void loadData(XmlPullParser parser) throws XmlPullParserException, IOException { 
     int eventType = parser.getEventType(); 
     while (eventType != XmlPullParser.END_DOCUMENT){ 
      int x = 0; 
      if(eventType == XmlPullParser.START_TAG){ 
       if(parser.getName() == "coordinates"){ 
        String coordinates = parser.nextText(); 
        StringTokenizer st = new StringTokenizer(coordinates, ",", false); 
        int y = 0; 
        locations.get(x).add(y, new location(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()))); 
        while(st.hasMoreTokens()){ 
         y++; 
         locations.get(x).add(y, new location(Double.parseDouble(st.nextToken().substring(2)), Double.parseDouble(st.nextToken()))); 
        } 
        x++; 
       } 
      } 
      eventType = parser.next(); 
     } 
    } 
} 

나는 그것이 내가 XML 제대로 분석 할 것을 확신이 코드 if(PointIsInRegion(Long, Lat, locations.get(i)))에 indexOutOfBoundException을 던져 그것을 실행하려고합니다. 나는 거기에서 어떤 에러도 볼 수 없다. 그러나 나는 그것이 나의 2D arraylist (나는 그것을 처음 사용하고있다)와 함께있을 것이라고 생각한다. 나는 정말로 필사적이다. 내가 뭘 잘못하고 있는지 말해 줄 수있어? 앞으로

+1

i == locations.size()'... – njzk2

+0

for (int i = 0; i <= locations.size(); i ++)는 (int i = 0; i

답변

2

for(int i = 0; i <= locations.size(); i++){에서

덕분에 i = locations.size() 때 경계 밖으로 이동합니다.

for (int i=0; i<locations.size(); i++) {으로 변경하십시오.

+0

ohhh ... 나는 작은 어리석은 실수 (아마 너무 피곤함)가 있다는 것을 알았다. 어쨌든 고마워. – horin

관련 문제