Java Example – Converting a Collection to an Array
The following example demonstrates how to use the list.add() and list.toArray() methods of the Java Util class to convert a collection to an array:
Main.java File
import
java
.
util
.*;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
List
<
String
list
=
new
ArrayList
<
String
(
);
list
.
add
(
"
Runoob
"
)
;
list
.
add
(
"
Tutorial
"
)
;
list
.
add
(
"
"
)
;
list
.
add
(
"
"
)
;
list
.
add
(
"
www.runoob.com
"
)
;
String
[
]
s1
=
list
.
toArray
(
new
String
[
0
]
)
;
for
(
int
i
=
0
;
i
<
s1
.
length
; ++
i
)
{
String
contents
=
s1
[
i
]
;
System
.
out
.
(
contents
)
;
}
}
}
The output of the above code is:
Runoob Tutorialwww.runoob.com
Java Examples