Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.8. Java code exercises  >   5.8.2. Code exercise 2

5.8.2. Code exercise 2
Last updated: 27 January 2013.

Write a method named getIndexOfMinimum(int[] array) that returns the index of the array argument's smallest value. If the array argument is null, the method returns -1.

public final class Practice {

   
public int getIndexOfMinimum(int[] array){
         
// the code goes here
   
}

}



public final class Practice {

   
public int getIndexOfMinimum(int[] array){
         
if(array == null)
               
return -1;

         
int indexOfMin = 0;
         
int minimum = array[0];

         
for(int i=0; i<array.length; ++i){
               
if(array[i] < minimum){
                     
minimum = array[i];
                      indexOfMin = i;
               
}
          }

         
return indexOfMin;
   
}

}


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.8. Java code exercises  >   5.8.2. Code exercise 2
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.8. Java code exercises  >   5.8.3. Code exercise 3

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7