Java Example – Array Expansion
The following example demonstrates how to expand an array after it has been initialized:
Main.java File
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
String
[
]
names
=
new
String
[
]
{
"
A
"
,
"
B
"
,
"
C
"
}
;
String
[
]
extended
=
new
String
[
5
]
;
extended
[
3
]
=
"
D
"
;
extended
[
4
]
=
"
E
"
;
System
.
arraycopy
(
names
,
0
,
extended
,
0
,
names
.
length
)
;
for
(
String
str
:
extended
)
{
System
.
out
.
println
(
str
)
;
}
}
}
The output of the above code is:
A
B
C
D
E
Java Examples