1

I always thought CharSequence[] and String[] were essential the same, however :

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.
4
  • nothing prevent an arrayadapter to use a CharSequence[] as a parameter. Commented Feb 20, 2013 at 15:33
  • When I try I get the following : The constructor ArrayAdapter<String>(Context, int, CharSequence[]) is undefined Commented Feb 20, 2013 at 15:36
  • 2
    why don't you use ArrayAdapter<CharSequence> ? Commented Feb 20, 2013 at 15:39
  • as @jlordo pointed out, you type your arrayadapter with String. Commented Feb 20, 2013 at 16:29

3 Answers 3

6

CharSequence is interface which String, StringBuffer, StringBuilder classes implements. So CharSequence can hold any of this implementation class's Object And CharSequence#toString returns String, try -

String[] mEntriesString = new String[mEntries.length];
int i=0;
for(CharSequence ch: mEntries){
    mEntriesString[i++] = ch.toString(); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

(not by me so i can only guess, but I'd say it's because it's useless and overkill?)
1

CharSequence is an Interface. String class implements that interface. 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

2 Comments

You can cast List to ArrayList, the ClassCastException will only happen if the referenced object is not of type ArrayList. See: List list = new ArrayList(); ArrayList a = (ArrayList) list; LinkedList b = (LinkedList) list; The first cast will work, the second one will fail.
@jlordo yes, I agree. I should've wrote that you cannot cast it SAFELY, unless you use instanceof operator
1

You can not cast reference CharSequence[] into 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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.