Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to Convert ArrayList<String> to String[] array ?

I'm working in the android environment and have tried the following code, but it doesn't seem to be working.

String [] stockArr = (String[]) stock_list.toArray();

If I define as follows:

String [] stockArr = {"hello", "world"};

it works. Is there something that I'm missing?
by

2 Answers

RoliMishra
Use like this.

List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");

String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);

for(String s : stockArr)
System.out.println(s);
kshitijrana14
Try this
String[] arr = list.toArray(new String[list.size()]);

Login / Signup to Answer the Question.