Array used as a paramater

By: Gayathri Viewed: 192 times  Printer Friendly Format    


Array used as a paramater here is a (nearly complete) program: import java.io.*; class ArrayOps { void print (int[] x) { for (int _____________;_____________________;____________) System.out.print (x[index] + ""); System.out.println(); } } class ArrayDemo { public static void main (String[] args) { ArraysOps operate = new ArraysOps(); // create an ArraysOps object int[] ar1={-20,19,1,5,-1,27,19,5}; System.out.print ("\n The array is:"); operate.print (ar1); // call the print() method of the object } }

The program defines a class called ArrayOps. ArrayOps contains a method print() to print out each element of an array. The parameter list is : int[] x This declares a formal parameter x which will be a reference to an array object. The caller is expected to supply a reference to an array as an actual parameter The program defines a class called ArrayDemo. ArrayDemo holds the static main() method main() creates an ArrayOps object,opearate. main() creates an array object,ar1 The print() method of the ArrayOps object is called with a reference to the array object ar1 as a parameter.

Using an array parameter Here is the completed program.The method is written using the parameter x to refer to the actual data that it will be supplied with.The method is written without reffereing to any particular data. The parameter x means " Whatever data is supplied whent he method starts to run."This might be different data at different times. import java.io.*; class ArrayOps { void print (int[] x) { for (int index=0;index


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial