[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Determine min array element no?

Posted on 1998-07-23
3
Medium Priority
?
232 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 150 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

872 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