将字符的ArrayList转换为字符串? [英] Converting ArrayList of Characters to a String?

查看:127
本文介绍了将字符的ArrayList转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中将 ArrayList< Character> 转换为 String
toString 方法将其返回为 [a,b,c] 字符串 - 我想要摆脱括号并将其存储为 abc

How to convert an ArrayList<Character> to a String in Java? The toString method returns it as [a,b,c] string - I want to get rid of the brackets and store it as abc.

推荐答案

您可以迭代通过列表并创建字符串。

You can iterate through the list and create the string.

String getStringRepresentation(ArrayList<Character> list)
{    
    StringBuilder builder = new StringBuilder(list.size());
    for(Character ch: list)
    {
        builder.append(ch);
    }
    return builder.toString();
}

除此之外, toString()返回ArrayList内容的人类可读格式。不值得花时间从中过滤掉不必要的字符。它的实现明天可能会改变,你必须重写你的过滤代码。

As an aside, toString() returns a human-readable format of the ArrayList's contents. It is not worth the time to filter out the unnecessary characters from it. It's implementation could change tomorrow, and you will have to rewrite your filtering code.

这篇关于将字符的ArrayList转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆