java - Smallest number in 2d array not working correctly -


i trying find smallest number in array.
when execute code, shows "23" smallest.
not able find mistake, can me out?

here code find smallest number in 2d array tried.
when execute code, shows "23" smallest.
not able find mistake.

public class arraymin {     public static void main(string args[])     {         int arr1[][]=new int[][]{{23,32,10,44},{44,33,22,11}};         int minvalue=arr1[0][0];         for(int i=0;i<arr1.length;i++)         {             for(int j=0;j<arr1.length;j++)             {                 if(arr1[i][j]<minvalue)                 {                    minvalue= arr1[i][j];                 }             }          }         system.out.println("min value is: "+minvalue);     } } 

you inner loop should be

for(int j=0;j<arr1[i].length;j++) 

since number of columns different number of rows.

and full code:

public class arraymin {   public static void main(string args[])   {     int arr1[][]=new int[][]{{23,32,10,44},{44,33,22,11}};     int minvalue=arr1[0][0];     for(int i=0;i<arr1.length;i++)     {         for(int j=0;j<arr1[i].length;j++)         {             if(arr1[i][j]<minvalue)             {                minvalue= arr1[i][j];             }         }      }     system.out.println("min value is: "+minvalue);   } } 

your current code checks first 2 columns in each row, out of 23,32,44 , 33, 23 smallest number.


Comments