There is problem of class casting. Please tell me how to cast this. I want to this ArrayList<CharSequence>
in my CharSequence[]
named Contacts
.
java.lang.ClassCastException
(in Android):
CharSequence[] Contacts;
List<CharSequence> contacts = new ArrayList<CharSequence>();
Contacts = (CharSequence[]) contacts.toArray();
for(CharSequence p : contacts) {
Log.i("log_tag", "AAAAAAAAAAAAAAAAAAA" + p);
}
Try this:
Contacts = contacts.toArray(new CharSequence[contacts.size()]);
Although, I would advise you to rename Contacts
to something else, perhaps contactsArray
. Two reasons for this: 1) Java variable names generally start with a lower-case letter. 2) You have another variable named contacts
, defining a second to be Contacts
will make the code much harder to read and understand since the two names differ only by the case of the first letter.
Relevant documentation:
toArray()
returns an array of type Object[]
. And Object[]
and CharSequence[]
are different types. If you want an array of CharSequence
, you must use the following code:
contactArray = contacts.toArray(new CharSequence[contacts.size()]);
See the javadoc for more details.
Note that Contacts
violates the Java naming conventions, and that having two variables, one named contacts
and the other named Contacts
, is really a bad idea (unless you want your code to be as unreadable as possible).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.