Java:将 char[] 转换为 CharSequence
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/299606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: convert a char[] to a CharSequence
提问by Chris Conway
What is the most direct and/or efficient way to convert a char[]
into a CharSequence
?
将 a 转换char[]
为 a的最直接和/或最有效的方法是CharSequence
什么?
采纳答案by Tom Hawtin - tackline
Without the copy:
没有副本:
CharSequence seq = java.nio.CharBuffer.wrap(array);
However, the new String(array)
approach is likely to be easier to write, easier to read and faster.
然而,这种new String(array)
方法可能更容易编写、更容易阅读和更快。
回答by jjnguy
A String
is a CharSequence
. So you can just create a new String
given your char[]
.
AString
是一个CharSequence
。所以你可以创建一个新的String
给定你的char[]
.
CharSequence seq = new String(arr);