Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList<String> to CharSequence[]

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?

like image 671
Laimoncijus Avatar asked Jun 13 '10 13:06

Laimoncijus


People also ask

Is String a CharSequence?

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.

What is CharSequence in Java?

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.


1 Answers

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] 
like image 122
BalusC Avatar answered Oct 28 '22 12:10

BalusC