ArrayList<String> to CharSequence[]

Laimoncijus picture Laimoncijus · Jun 13, 2010 · Viewed 58.3k times · Source

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?

Answer

BalusC picture BalusC · Jun 13, 2010

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]