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
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?            

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...

Question by:epifanio67
    LVL 35

    Assisted Solution

    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.
    LVL 8

    Assisted Solution

    LVL 8

    Accepted Solution


    Author Closing Comment

    Thank you experts...

    I appreciate your help...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Top Freelancers to Complete Java Projects

    Source the talented Expert Exchange community
    for top quality work on your Java projects.

    Hire the best. Collaborate easily. Get quality work.

    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    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 …
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now