Sunday, August 2, 2009

How can I get the greatest integer in an array in Java?

I have a variety of integers in an array. How can I most easily find the greatest one among them?

How can I get the greatest integer in an array in Java?
This should get you started:





// algorithm to find and display largest item in array





int largest = numbers[0];


int pos = 1;


int SIZE = 10;





while (pos %26lt; SIZE)


{


if (numbers[pos] %26gt; largest)


{


largest = numbers[pos];


}


pos = pos + 1;


}





The variable "largest" contains the largest integer.
Reply:therss no greasted interger
Reply:int largest = arrInt[0];


int ctr = 1;


while ( ctr %26lt;= arrInt.length-1)


{


if (arrInt [ctr] %26gt; largest)


{


largest = arrInt[ctr];


}


ctr++;


}


System.out.print("The largest integer in the list is: " + largest);





//largest is the variable that will hold the largest integer.


//arrInt is your array


//arrInt.lenght determines the size of your array.
Reply:either loop through them and do:


if (number %26gt; highest) { highest = number; }





or use Array.sort() and get the last element. (much slower)

garden design

No comments:

Post a Comment