Tuesday 27 October 2015

Java Tutorial : Java Array (Java Array Pass to Method)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=ZvNr3KfSv1g&list=UUhwKlOVR041tngjerWxVccw

ArrayDemo.java
/*
 * We can pass the java array to method so that we can reuse the same logic on any array.
 */

class ArrayDemo
{
    public static void main(String[] args)
    {
        int[] intArray1 = new int[5];// declaration and instantiation

        intArray1[0] = 110;// initialization
        intArray1[1] = 220;
        intArray1[2] = 750;
        intArray1[3] = 130;
        intArray1[4] = 150;

        System.out.print("Max value in intArray1 :");
        max(intArray1);
        
        
        int[] intArray2 = new int[3];// declaration and instantiation

        intArray2[0] = 45;// initialization
        intArray2[1] = 50;
        intArray2[2] = 10;
        
        System.out.print("Max value in intArray2 :");
        max(intArray2);
        
    }

    private static void max(int[] intArray)
    {
        int max = intArray[0];
        for (int i = 0; i < intArray.length; i++)
        {
            if (max < intArray[i])
            {
                max = intArray[i];
            }
        }
        System.out.println(max);
    }

}
Output
Max value in intArray1 :750
Max value in intArray2 :50
To Download ArrayDemoPassToMethodApp Project Click the below link
https://sites.google.com/site/javaee4321/java/ArrayDemoPassToMethodApp.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment