Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Converting a Collection to an Array

Java Example – Converting a Collection to an Array

Java Examples Java Examples

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

.

print

(

contents

)

;

}

}

}

The output of the above code is:



Runoob Tutorialwww.runoob.com

Java Examples Java Examples