Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 5th, 2006, 05:15 PM
Registered User
 
Join Date: Nov 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default urgent need help with array

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]);
    }

  }



 
Old January 10th, 2006, 06:07 PM
Authorized User
 
Join Date: Aug 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old January 14th, 2006, 06:44 AM
Authorized User
 
Join Date: Dec 2005
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old April 23rd, 2009, 08:28 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Quote:
Originally Posted by adarsh83 View Post
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
No. This doesn't make any sense. You know how heavy the sort operation is?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convering a String Array to an Integer array nkrust C# 9 November 17th, 2010 12:02 PM
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
error when sorting an Array of Array nancy VBScript 2 February 17th, 2005 12:57 PM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM
Array to Array comparison pavel Pro VB 6 0 March 24th, 2004 06:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.