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

Time:12-18

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?

CodePudding user response:

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

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

CodePudding user response:

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.

  • Related