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

Categories

How can I fill a array with the data provided by one List?

For example, I have a List with Strings:

List l = new ArrayList<String>();
l.add("a");
l.add("b");
l.add("c");

then I want to copy this data into a String array:

String[] array = ?
See Question&Answers more detail:os

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

1 Answer

There's a toArray() method in list...
You have to first allocate an array of an appropriate size (typically list.size()), and then pass it to the toArray method as a parameter. The array will be initialized with the list content.

For example:

String[] myArray = new String[myList.size]();
myList.toArray(myArray);

You can also do the new call inside the parentheses of toArray


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...