简体   繁体   中英

How to change elements in the ArrayList to a different object type?

I have an ArrayList<Character> . How do I change the elements inside the array into integers? The code is in Java.

Example:

private ArrayList<Character> arrayList;

I tried Integer.parseInt() like this:

Integer.parseInt(arrayList, arrayList.get(0));

But the parseInt only works for strings.

文档 ,您可以只使用.charValue()并将其转换为int

(int)(arrayList.get(0).charValue());

You are trying to convert a character to int while the parseInt method works only for Strings. Try this:

Integer.parseInt(arrayList.get(0).toString());

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.

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