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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:47:25  来源:igfitidea点击:

Java: convert a char[] to a CharSequence

javastring

提问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 Stringis a CharSequence. So you can just create a new Stringgiven your char[].

AString是一个CharSequence。所以你可以创建一个新的String给定你的char[].

CharSequence seq = new String(arr);