今天整理以前的一些零碎代码,把一个for each 循环+可变参数的小例子(JDK5)和大家分享一下
public class VariableArgs {
public static void main(String[] args) {
VariableArgs test = new VariableArgs();
test.show( " 1 " , " 2 " );
test.show( " 1 " , " hermit " , " 3 " );
test.showObject( 123 , " hermit " );
test.addT( 1 , 2 , 3 , 4 );
}
public void show(String...strings) {
System.out.println(strings.length);
for (String str : strings) {
System.out.print(str + " " );
}
System.out.println();
}
public void showObject(Object...objects) {
System.out.println(objects.length);
for (Object obj : objects) {
System.out.print(obj.toString() + " " );
}
System.out.println();
}
public void addT( int ...obj) {
System.out.println(obj.length);
int rs = 0 ;
for ( int x : obj) {
rs += x;
}
System.out.println(rs);
}
}
public class VariableArgs {
public static void main(String[] args) {
VariableArgs test = new VariableArgs();
test.show( " 1 " , " 2 " );
test.show( " 1 " , " hermit " , " 3 " );
test.showObject( 123 , " hermit " );
test.addT( 1 , 2 , 3 , 4 );
}
public void show(String...strings) {
System.out.println(strings.length);
for (String str : strings) {
System.out.print(str + " " );
}
System.out.println();
}
public void showObject(Object...objects) {
System.out.println(objects.length);
for (Object obj : objects) {
System.out.print(obj.toString() + " " );
}
System.out.println();
}
public void addT( int ...obj) {
System.out.println(obj.length);
int rs = 0 ;
for ( int x : obj) {
rs += x;
}
System.out.println(rs);
}
}