Solved

Determine min array element no?

Posted on 1998-07-23
3
227 Views
Last Modified: 2010-03-30
I want to know is there any method provided in Java to help us find the array element no. which has the smallest value among all. Eg. A[0] = 1, A[1] = 3, A[2] = 0. Therefore, the smallest value is '0', and the element no is '2'. Thus, can I get the element no '2' by using any easy method.

Thanks!
0
Comment
Question by:yuho
3 Comments
 

Accepted Solution

by:
tomd012698 earned 50 total points
ID: 1227842
 There is no pre-existing method that will accomplish what you want, but you could write one yourself.  Try using this code.

int findSmallestIndex( int arr[], int length )
{
  int nSmallVal = Integer.MAX_VALUE;
  int nSmallIndex = 0;

  for ( int n = 0; n < length; n++ )
  {
    if ( arr[ n ] < nSmallVal )
    {
       nSmallVal = arr[ n ];
       nSmallIndex = n;
    }
  }
  return nSmallIndex;
}

  When multiple entries have the same value, the first one encountered will always win.  This is a brute force approach to this problem, but will be acceptable as long as you don't need to store a very large number of elements, and will not be calling this function a large number of times.  In those cases, I would suggest presorting the array, and simply looking at the value of the first element (if this fits into your needs).
0
 
LVL 1

Expert Comment

by:dryang
ID: 1227843
hi yuho,

you can dig around for books with the kind of sorting algorithms you need, even to sorting objects. Try this one:

Java Algorithms by Scott Robert Ladd,Mc Graw Hill 1997
0
 

Author Comment

by:yuho
ID: 1227844
Thanks!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet example 17 51
printf performancy 11 69
Selenium docs api java index 3 48
Tagging and Merging on Branch 1 29
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question