简体   繁体   中英

How to convert ArrayList<String> into string in Java and use it in DirectionsAPI url for waypoints

I'm using DirectionsAPI in Android Studio and I want to make my ArrayList of waypoints correct so I can use it in the URL.

So, I have

ArrayList<String> y = new ArrayList<>();
    for(int i=0; i< mMarkerPoints.size() ; i++){
        double lat = mMarkerPoints.get(i).latitude;
        double lon = mMarkerPoints.get(i).longitude;

        String point = String.valueOf(lat) +","+ String.valueOf(lon);

        y.add(point);

    }
    String list = y.toString().replace(",", "|");

Here I am converting my ArrayList into a String so that it's easier to use it in the url. In the url, the waypoints need to have this structure:

&waypoints=41.1784687,-8.608977699999999|41.1802785,-8.596998899999999

How do I arrange my string so that I am able to use | as a divider?

the comma , is the syntax to separate values. You cannot change the syntax of the java language as you wish. It would be a compile error.

The | is a logical OR. It has a total different meaning in the language than you are assuming.

You can use String.join to easily join a List<String> with a delimiter of your choice:

String list = String.join("|", y);

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