Java String Concatenation

后端 未结 7 1713
無奈伤痛
無奈伤痛 2021-01-21 14:45

I have a issue with my java code. i asked the same question yesterday. I got answer but sorry it was my fault. My question is not clear.

I have code looks like this:

相关标签:
7条回答
  • 2021-01-21 15:13

    Additions to Thierry-Dimitri Roys response.

    StringBuilder sb = new StringBuilder();
    ProximityTarget prox;
    float latitude;
    float longitude;
    float radius;
    
    //Use for-each if you process all elements
    for(Target target : geo.getTargets()){
    
      //Use literal string first to avoid NullPointerException
      if("ProximityTarget".equalsIgnoreCase(target.getTargetType())){
        prox        = (ProximityTarget)geo.getTargets(i);
        latitude    = prox.getGeoPoint().getLatitudeInMicroDegrees()); 
        longitude   = prox.getGeoPoint().getLongitudeInMicroDegrees());
        radius      = prox.getRadiusDistanceUnits();
    
        //Checking sb every loop is not a good practice.
        //Instead try removing first delimeter after for-loop
        sb.append("|circle:" + longitude + ":" + latitude + ":" + radius);
      } 
    }
    
    //AFAIK StringBuilder has no isEmpty Method.
    //So use length method
    if (sb.length() > 0){
        sb.deleteCharAt(0);
    }
    
    String result = sb.toString();  
    
    0 讨论(0)
提交回复
热议问题