Results 1 to 2 of 2
  1. #1
    Cod is offline Member
    Join Date
    Nov 2009
    Honolulu, HI
    Rep Power

    Default Finding the Smallest Element in an Array

    I'm trying to write a portion of code to find the smallest element of an array. The code below works; however, it doesn't place a negative symbol in front of the element (if its negative). For example, if the smallest element is -2, my code returns "2".

    Java Code:
    int minIndex = 0;      
    for (int i = 1; i < a.length; i++) {
      if (a[minIndex] > a[i])
      minIndex = i;
    Any guidance is greatly appreciated.
    Last edited by Cod; 02-20-2011 at 02:31 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Rep Power


    Do you want the index of the smallest element, or the value of the smallest element? Currently you are getting the index of the an example, for an array {0,-1,-2}, minIndex will be 2....use a[minIndex] to get the value.

Similar Threads

  1. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  2. Array[] get smallest/largest value
    By bobocheez in forum New To Java
    Replies: 13
    Last Post: 09-09-2010, 11:16 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Problem with finding the max element
    By SMHouston in forum New To Java
    Replies: 39
    Last Post: 09-13-2009, 10:34 PM
  5. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts