// ParametersVaryingInNumber.java Copyright (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2005-07-15 File created. // 2005-08-03 Last modification. /* With the ... notation it is possible to declare a method that accepts a varying number of parameters of the same type. Only the last formal parameter of a method can be declared this way. The last parameters of the methods in this program work like arrays when they are declared with the three dots ... The corresponding C# program is ParamsKeywordTest.cs */ import java.util.* ; class ParametersVaryingInNumber { static void print_integers( int... given_integers ) { System.out.print( "\n Given integers: " ) ; for ( int integer_index = 0 ; integer_index < given_integers.length ; integer_index ++ ) { System.out.print( " " + given_integers[ integer_index ] ) ; } } static void print_string_and_integers( String given_string, int... given_integers ) { System.out.print( "\n " + given_string + " " ) ; for ( int integer_in_array : given_integers ) { System.out.print( " " + integer_in_array ) ; } } public static void main( String[] not_in_use ) { int some_int_variable = 999 ; int another_int_variable = 444 ; print_integers() ; print_integers( 1111 ) ; print_integers( 3, 88, 77, 2, 5 ) ; print_integers( some_int_variable, another_int_variable ) ; int[] array_of_integers = { 44, 999, 66, 11 } ; print_integers( array_of_integers ) ; print_string_and_integers( "xxx" ) ; print_string_and_integers( "yyy", 1111 ) ; print_string_and_integers( "zzz", 3, 88, 77, 2, 5 ) ; print_string_and_integers( "www", array_of_integers ) ; } }