Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

java - 转换ArrayList <String> 到String []数组[重复](Convert ArrayList<String> to String[] array [duplicate])

This question already has an answer here:

(这个问题已经在这里有了答案:)

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

(我在android环境中工作,并尝试了以下代码,但似乎不起作用。)

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

If I define as follows:

(如果我定义如下:)

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

it works.

(有用。)

Is there something that I'm missing?

(有什么我想念的吗?)

  ask by locoboy translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...