/**
*
* @author hitdong
* @version 1.0
* @since 2010.1.1
*/
public class Main
{
/**
* @param args the command line arguments
*/
public static
void
main
(
String
[]
args
)
{
/*
* 不传递参数
*/
printArgs
();
System
.
out
.
println
(
"--------------------------"
);
String arg1
=
"This is the first args"
;
String arg2
=
"This is the second args"
;
/*
* 并列地传给多个参数
*/
printArgs
(
arg1
,
arg2
);
System
.
out
.
println
(
"--------------------------"
);
String
[]
argsArray
=
new
String
[]{
arg1
,
arg2
};
/*
* 以数组方式传递多个参数
*/
printArgs
(
argsArray
);
System
.
out
.
println
(
"--------------------------"
);
}
/*
*些函数接受类型为String的个数可变的参数,形参varargs是个数组
*/
public static
void
printArgs
(
String
...
varargs
){
int
argsLength
=
varargs
.
length
;
if
(
argsLength
==
0){
System
.
out
.
println
(
"Give no args"
);
}
else
{
System
.
out
.
println
(
"the args number is:"
+
varargs
.
length
);
}
for
(
int
i
=
0;
i
<
argsLength
;
i
++)
{
System
.
out
.
println
(
"args "
+
i
+
" is "
+
varargs
[
i
]);
}
}
}
运行结果如下
1
2
3
4
5
6
7
8
9
10
Give no args
--------------------------
the args number
is:
2
args
0
is This is the first args
args
1
is This is the second args
--------------------------
the args number
is:
2
args
0
is This is the first args
args
1
is This is the second args
--------------------------