Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Determine min array element no?

Posted on 1998-07-23
3
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

618 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