简体   繁体   English

转换CharSequence []和CharSequence []

[英]Converting CharSequence[] and CharSequence[]

I always thought CharSequence[] and String[] were essential the same, however : 我一直认为CharSequence []和String []是必不可少的,但是:

I have some code that has the following : 我有一些具有以下内容的代码:

CharSequence[] mEntries;
...
String[] mEntriesString = (String[]) mEntries;
ListAdapter adapter = new ArrayAdapter<String>(getContext(), R.layout.two_lines_list_preference_row, mEntriesString)

When the code runs I get 当代码运行时,我得到

java.lang.ClassCastException: java.lang.CharSequence[] cannot be cast to java.lang.String[]

So two questions ? 有两个问题?

  • why can this cast not happen 为什么这种转换不会发生
  • why does the ArrayAdapter not allow for a CharSequence[] in it's constructor. 为什么ArrayAdapter不允许在其构造函数中使用CharSequence []。

CharSequence is interface which String , StringBuffer , StringBuilder classes implements. CharSequenceStringStringBufferStringBuilder类实现的接口。 So CharSequence can hold any of this implementation class's Object And CharSequence#toString returns String , try - 因此, CharSequence可以容纳此实现类的任何Object,并且CharSequence#toString返回String ,尝试-

String[] mEntriesString = new String[mEntries.length];
int i=0;
for(CharSequence ch: mEntries){
    mEntriesString[i++] = ch.toString(); 
}

CharSequence is an Interface. CharSequence是一个接口。 String class implements that interface. String类实现该接口。 So, you cannot cast it to String , the same way you cannot cast List to ArrayList , because it doesn't have to be an instance of that concrete class 因此,您不能将其StringString ,也无法将List ArrayListArrayList ,因为它不必是该具体类的实例

You can not cast reference CharSequence[] into String[] . 您不能将引用CharSequence[]转换为String[]

You can cast it only in that situation: 您只能在这种情况下进行投射:

        CharSequence[] charSequencesAsString = new String[] { "test" };
        String[] result = (String[]) charSequencesAsString;
        System.out.println(Arrays.toString(result));

Safe way to solve your problem: 解决问题的安全方法:

public static void main(String[] args) {

    CharSequence[] charSequencesAsString = new String[] { "test" };
    CharSequence[] charSequencesAsCharSequence = new CharSequence[] { "test" };
    CharSequence[] charSequencesAsStringBuilder = new StringBuilder[] { new StringBuilder("Test") };

    String[] stringsFromStrings = convertToStringArray(charSequencesAsString);
    String[] stringsFromCharSequence = convertToStringArray(charSequencesAsCharSequence);
    String[] stringsFromStringBuilder = convertToStringArray(charSequencesAsStringBuilder);

    System.out.println("Same array after conversion: " + (stringsFromStrings == charSequencesAsString));
    System.out.println("Same array after conversion: " + (stringsFromCharSequence == charSequencesAsCharSequence));
    System.out.println("Same array after conversion: " + (stringsFromStringBuilder == charSequencesAsStringBuilder));
}

public static String[] convertToStringArray(CharSequence[] charSequences) {
    if (charSequences instanceof String[]) {
        return (String[]) charSequences;
    }

    String[] strings = new String[charSequences.length];
    for (int index = 0; index < charSequences.length; index++) {
        strings[index] = charSequences[index].toString();
    }

    return strings;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM