简体   繁体   中英

Dynamically rename objects in java?

I am developing a web application using jsf/primefaces. In my application i am pushing numerous polygons onto an gmap overlay. However when i push the polygon onto the overlay, it merges into one big polygon because the object name is static at Polygon polygon. My question is how can rename the polygon on iteration and change to polygon objects name from from polygon to polygon1, polygon2, polygon3, etc.Here is a snippet of my code where i am trying to push polygons onto the overlay.

Polygon polygon = new Polygon();

                while (rs.next()) {
                    HashMap<Integer, Polygon> map;
                    map = new HashMap<Integer, Polygon>();
                    int id = rs.getInt(1);
                    String value = rs.getString(2);

                    String valuereal = value.replaceAll("[^0-9 .,-]+", "");
                    ArrayList<String> myList = new ArrayList<String>(Arrays.asList(valuereal.split(",")));
                    myList.remove(myList.size() - 1);
                    for (String coordString : myList) {

                        String[] parts = coordString.split(" ");

                        String latS = parts[0];
                        String lngS = parts[1];

                        double lat = Double.parseDouble(latS);
                        double lng = Double.parseDouble(lngS);

                        LatLng coord = new LatLng(lat, lng);
                        polygon.getPaths().add(coord);
                        map.put(id, polygon);

                    }

                    polygon.setStrokeColor("#000000");
                    polygon.setFillColor("#009900");
                    polygon.setStrokeOpacity(2.0);
                    polygon.setFillOpacity(3.0);
                    System.out.println(map.get(id).getPaths());

                    polygonModel.addOverlay((Polygon) map.get(id));

Your problem is that you have instantiated the polygon object outside the loop. To separate them out you don't need to create 'polygon1, plygon2 .. and so on'.

Cleaner way would be to create a Array list outside loop, craete a new 'polygon' inside loop and keep pushing the polygon in the array list. Advantage is you can later use this ArrayList to iterate and do whatever with the polygons

 ArrayList <Polygon> polygons = new ArrayList<Polygon>();

  while (rs.next()) {
                    Polygon polygon = new Polygon();
                         .
                         .
                         .
                         .
                    for(....){
                        polygon.getPaths().add(coord);
                        polygons.add(polygon)
                    }
                    // now add all polygons to map

                    for (Polygon p : polygons) {
                         map.put(id, p);
                     }
   } // end of while

The above is just a snippet, but should give you an idea of what I mean. Please forgive any syntax errors, its been a while since I spoke any Java.

This will give you separate polygons objects added to map, however I am not too certain about the 'id' you add it with to the map object. That seems to be business logic. However you get the idea. At the max, you may have to add each polygon with new ID, which will push the map.put loop outside the while

Looks like you are repeatedly updating the same polygon object as it's being instantiated outside of your while loop. Try newing it up inside the while loop eg

Polygon polygon = new Polygon();
while (rs.next()) {
    HashMap<Integer, Polygon> map;

becomes...

while (rs.next()) {
    Polygon polygon = new Polygon();
    HashMap<Integer, Polygon> map;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM