尝试转换为CharSequence []时,我一直收到此错误。
方法toArray(CharSequence [])未定义类型String
这是我的代码
CharSequence[] cs = abbrev.toArray(new CharSequence[abbrev.length()]);
缩写只是将句子分成第一个字符 (Hello World - > HW)
String[] result = matches.toString().split("\\s+");
// The string we'll create
String abbrev = "";
// Loop over the results from the string splitting
for (int i = 0; i < result.length; i++){
// Grab the first character of this entry
char c = result[i].charAt(0);
// If its a number, add the whole number
if (c >= '0' && c <= '9'){
abbrev += result[i];
}
// If its not a number, just append the character
else{
abbrev += c;
}
}
任何想法?
由于
答案 0 :(得分:0)
由于abbrev
是一个字符串,因此您无法在其上调用toArray
。首先将其转换为CharSequence
。
例如,
CharSequence[] abbrevCs = abbrev; // it's converted automatically
CharSequence[] cs = abbrevCs.toArray(new CharSequence[abbrev.length()]);
答案 1 :(得分:0)
您不必将String
转换为CharSequence
,因为String
是CharSequence
。您只需将String
对象分配给CharSequence
对象,转换就是隐式的。有关详情,请参阅此处:How to convert a String to CharSequence?