What would be the easiest way to make a CharSequence[]
out of ArrayList<String>
?
Sure I could iterate through every ArrayList
item and copy to CharSequence
array, but maybe there is better/faster way?
A String already is a CharSequence. The String class implements the CharSequence interface. @JeffScottBrown the question actually makes sense, it's a legitimate wonder to anyone reading through the Android or Java doc and missing the detail that CharSequence is not a class.
A CharSequence is a readable sequence of char values. This interface provides uniform, read-only access to many different kinds of char sequences. A char value represents a character in the Basic Multilingual Plane (BMP) or a surrogate. Refer to Unicode Character Representation for details.
You can use List#toArray(T[])
for this.
CharSequence[] cs = list.toArray(new CharSequence[list.size()]);
Here's a little demo:
List<String> list = Arrays.asList("foo", "bar", "waa"); CharSequence[] cs = list.toArray(new CharSequence[list.size()]); System.out.println(Arrays.toString(cs)); // [foo, bar, waa]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With