?
Solved

Java: in a loop, is there a way to get the lowest and highest number? the loop is of type Integer

Posted on 2012-09-04
4
Medium Priority
?
512 Views
Last Modified: 2012-09-04
Hello Experts,

In a loop, is there a way to get the lowest and highest number in a loop?

for (int y = 0; y < 10; y++) {
      //how do I print/get/assign the lowest number on this loop?
      //how do I print/get/assign the highest number on this loop?            
}

notes:
y is an arrayList<Integer>
the size of y will vary, it may  begin at 50 to 500,000

I want to get the lowest number to assign it to a variable and
I want to get the highest number to assign it to a variable....

In advance, thanks for your help...

Regards,
0
Comment
Question by:epifanio67
  • 2
4 Comments
 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 668 total points
ID: 38366273
Firstly, you can't have 'y' as both your ArrayList AND you loop counter, ie. change the for loop to something like, for (int i = 1; i < y.size(); i++) (Also, note that we are starting the loop from index 1 rather than 0, that is intentional, see below.

Now, to do want you want you will need to declare two variables before the loop, say int lowestNumber and int highestNumber, and assign them both the first number in your ArrayList, eg. int lowestNumber = y.get(0);

Now the loop starts from the second number, index 1 in the list (see above), and compares it to the two variables that we declared above, so if the number that the loop is currently looking at is less than lowestNumber then store this new number in lowestNumber, and we do the same for highestNumber but the comparison uses greater than, obviously.

Then by the end of the loop, lowestNumber and highestNumber will contain what you are after.


Assuming that this could be an academic question, I will leave the actual coding up to yourself, but have a go and if there are still issues, let us know and we can help further.
0
 
LVL 9

Assisted Solution

by:Manikandan Thiagarajan
Manikandan Thiagarajan earned 1332 total points
ID: 38366296
0
 
LVL 9

Accepted Solution

by:
Manikandan Thiagarajan earned 1332 total points
ID: 38366302
0
 

Author Closing Comment

by:epifanio67
ID: 38366305
Thank you experts...

I appreciate your help...
Regards,
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 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