Page 351 - CITS - Computer Software Application -TT
P. 351

COMPUTER SOFTWARE APPLICATION - CITS




              Passing Array to a Method in Java
              We can pass the java array to method so that we can reuse the same logic on any array.

              Let’s see the simple example to get the minimum number of an array using a method.


              //Java Program to demonstrate the way of passing an array
              //to method.
              class Testarray2{
              //creating a method which receives an array as a parameter
              static void min(int arr[]){
              int min=arr[0];
              for(int i=1;i<arr.length;i++)
               if(min>arr[i])
                min=arr[i];

              System.out.println(min);
              }

              public static void main(String args[]){
              int a[]={33,3,4,5};//declaring and initializing an array
              min(a);//passing array to method
              }}
              Output
              3


              Multidimensional Array in Java
              In such case, data is stored in row and column based index (also known as matrix form).


              Syntax to Declare Multidimensional Array in Java

              dataType[][] arrayRefVar; (or)
              dataType [][]arrayRefVar; (or)
              dataType arrayRefVar[][]; (or)
              dataType []arrayRefVar[];
              Example to instantiate Multidimensional Array in Java

              int[][] arr=new int[3][3];//3 row and 3 column
              Example to initialize Multidimensional Array in Java


              arr[0][0]=1;
              arr[0][1]=2;
              arr[0][2]=3;
              arr[1][0]=4;
              arr[1][1]=5;
              arr[1][2]=6;
              arr[2][0]=7;




                                                           338

                              CITS : IT&ITES - Computer Software Application - Lesson 85 - 93
   346   347   348   349   350   351   352   353   354   355   356