How to

Quick Tip – How to convert a Java List to a Java String

Hello ! I’m Xavier Jouvenot and in this small post, I am going to explain how to convert a Java List<Integer> to a Java String.

Self promotion: You can find other articles on Android development on my website 😉

Solution

For the people who only want the solution, to quickly copy-paste it in there code, here it is 😉

List<Integer> my_list;
// some code to fill the list ...

StringBuilder sb = new StringBuilder();
for (int i = 0; i < my_list.size(); ++i)
{
    sb.append(my_list.get(i));
    if(i != my_list.size() - 1)
    {
        sb.append(", ");
    }
}
String s = sb.toString();

Explanation

If you read this, it may be that you want to understand how the previous solution achieve the goal of transforming the List<Integer> into a String, and this is what I am going to try to explain. 🙂

First of all, we create the list and fill it with whatever integer you want. And we create a StringBuilder which is going to help us doing the transformation.

Then, we transfer the information from the List<Integer> to the StringBuilder and format it how we want to. In our case, we are separating the each numbers of the list with a comma and a space.

Finally, we create a String with all the information we want, by using the method toString of StringBuilder.


Thank you all for reading this article, And until my next article, have a splendid day 😉

Interesting links

Votre commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l’aide de votre compte WordPress.com. Déconnexion /  Changer )

Photo Google

Vous commentez à l’aide de votre compte Google. Déconnexion /  Changer )

Image Twitter

Vous commentez à l’aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l’aide de votre compte Facebook. Déconnexion /  Changer )

Connexion à %s