Arrays manipulations

Monday, June 22, 2015

With only one loop get a identical 2 numbers in array of 100 diffrent number but there are only 2 identical

// code here

Get Max value of an array
    int GetMax()
    {
        int[] arr = {1,2,20,3,4,5,100,5,9};
        int max = arr[0];
      
        for(int i = 0; i < arr.length; i++)
        {
            if (arr[i] > max) max = arr[i];
        }
        return max;  
    }
   
    // Using for-each
    int GetMax2()
    {
        int[] arr = {1,2,20,3,4,5,100,5,9};
        int max = arr[0];
      
        for(int e : arr)
        {
            if (e > max) max = e;
        }
        return max;  
    }
   

compare two arrays
    boolean CompareTwoArrays()
    {
        int[] arr1 = {1,2,20};
        int[] arr2 = {1,2,20};
      
        if (arr1.length != arr2.length) return false;
        for(int i = 0; i < arr1.length; i++)
        {
        if(arr1[i] != arr2[i])
        {
            return false;
        }
        }
        return true;  
    }
    

You Might Also Like

0 comments

Popular Posts

Like us on Facebook

Flickr Images