Subject: urgent need help with array
Posted By: had13 Post Date: 1/5/2006 4:15:57 PM
URGENT FOR EXAM STUDY!!

how do i search this array(numArray) for the largest and smallest value (or min and max value) and then diplay them.


static void main(String args[])
  {
    int [] numArray = {7,10,255,46,59};
    int largest;
    int smallest;
    
    for (int i=0; i < numArray.length; i++)
    {
      numArray[i] = numArray[i] +5;
      screen.print(numArray[i]);
    }
    
  }



Reply By: RahulSapkal Reply Date: 1/10/2006 5:07:45 PM
Here it goes,

static void main(String args[])
  {
    int [] numArray = {7,10,255,46,59};
    int largest = numArray[0];
    int smallest = numArray[0];
    
    for (int i=1; i < numArray.length; i++)
    {
      if(numArray[i] > largest)
      {
          largest = numArray[i];
      }

      if(numArray[i] < smallest)
      {
          smallest = numArray[i];
      }
    
    }
    
  }

rahul@javareference.com
--------------------------
http://www.javareference.com
Reply By: adarsh83 Reply Date: 1/14/2006 5:44:13 AM
Could do this way too:

static void main(String args[])
{
    int [] numArray = {7,10,255,46,59};

    Arrays.sort(numArray);

    System.out.println("Smallest : "+numArray[0]);
    System.out.println("Largest : "+numArray[numArray.length - 1]);
}

________
Adarsh

Go to topic 13430

Return to index page 395
Return to index page 394
Return to index page 393
Return to index page 392
Return to index page 391
Return to index page 390
Return to index page 389
Return to index page 388
Return to index page 387
Return to index page 386