Solved

How do I get highest and lowest value in an array of Int's?

Posted on 2004-10-26
402 Views
Last Modified: 2006-11-17
Hi,

I have an array of int's, and I would like to get the highest value and the lowest value, by applying a method to the array, such as for example:

lowest = myArray.someMethod1()

highest = myArray.someMethod2()
0
Question by:jakekula
    20 Comments
     
    LVL 92

    Accepted Solution

    by:
    Arrays.sort(myArray);
    int max = myArray[myArray.length-1]
    int min = myArray[0];
    0
     
    LVL 37

    Assisted Solution

    by:zzynx
    By sorting it (using java.util.Arrays.sort()) and taking the first and the last.

    0
     
    LVL 5

    Expert Comment

    by:Naeemg
    Try this code.
    suppose ur array has three values int type, '3', '2' and '4'.

        int[] intValues = new int[3];
        intValues[0] = 3;
        intValues[1] = 2;
        intValues[2] = 4;
        Arrays.sort(intValues);
        int max = intValues[intValues.length-1];
        int min = intValues[0];


    Naeem Shehzad Ghuman
    0
     
    LVL 3

    Expert Comment

    by:RuadRauFlessa
    Eather that or you can simply loop through the array and store the highest value each time:

    public intgetHighest() {
      int high = [some verry verry low number]
        for (int i = 0; i < array.length; i++) {
          if (array[i] > high) high = array[i];
        }
      return high;
    }

    public int getLowest() {
      int low = [some verry verry high number]
        for (int i = 0; i < array.length; i++) {
          if (array[i] < low) low = array[i];
        }
      return low;
    }
    0
     
    LVL 3

    Expert Comment

    by:RuadRauFlessa
    Naeemg you could do that but you would be wasting valuable processing time since the sorting method does exactly what my code does and what happens if you don't want the array to be sorted?
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    jakekula,
    if you would think about looping through the array like RuadRauFlessa suggests,
    you'd better make sure to only loop once through it remembering the the lowest and the highest in one loop.
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    >> you would be wasting valuable processing time since the sorting method does exactly what my code does
    since? I don't see it like that.

    >>what happens if you don't want the array to be sorted?
    Then do the sort on a copy
    0
     
    LVL 3

    Expert Comment

    by:RuadRauFlessa
    if yo do the sort on the copy you waste memory as well.

    public class MyArray {
      public static final LOW_VALUE = 0;
      public static final HIGH_VALUE = 1;
      private int[] array = null;

      public MyArray(int[] array) {
        this.array = array;
      }

      public int getHighLow() {
        int[] ret = new int[2]
          ret[0] = [some verry verry low number];
          ret[1] = [some verry verry high number];
          for (int i = 0; i < array.length; i++) {
            if (array[i] > high) high = array[i];
          }
        return high;
      }

      public static void main(String[] args) {
        MyArray myArray = new MyArray([pass it an array]);
        int[] ret = myArray.getHighLow();
        System.out.println("High = " + ret[MyArray.HIGH_VALUE]);
        System.out.println("Low = " + ret[MyArray.LOW_VALUE]);
      }  
    }

    This would be better
    0
     
    LVL 3

    Expert Comment

    by:RuadRauFlessa
    or

      public static int getHighLow(int[] array) {
        int[] ret = new int[2]
          ret[0] = [some verry verry low number];
          ret[1] = [some verry verry high number];
          for (int i = 0; i < array.length; i++) {
            if (array[i] > high) high = array[i];
          }
        return high;
      }

    if you do not want another object in memory
    0
     
    LVL 92

    Expert Comment

    by:objects
    you could also use the following in your loop:

    hi = Math.max(hi, myArray[i]);
    lo = Math.min(lo, myArray[i]);
    0
     
    LVL 3

    Expert Comment

    by:RuadRauFlessa
    Then you don't need a loop

      public static int getHighLow(int[] array) {
        int[] ret = new int[2]
          ret[0] = Math.max(hi, array[i]);
          ret[1] = Math.min(lo, myArray[i]);
        return high;
      }

    Which would be faster still.

    Nice one objects, I missed that one.
    0
     
    LVL 92

    Expert Comment

    by:objects
    > Then you don't need a loop

    you still need the loop
    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    And after all this havoc, I presume this is homework.
    In real life you keep your data in the order that you need it in .......
    ;JOOP!
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    >> Then you don't need a loop
    Of course you need one. What is the variable "i" in your code RuadRauFlessa?
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    > In real life you keep your data in the order that you need it in .......

    Or keep track of the highest and lowest value added to the array as they are added...
    0
     
    LVL 3

    Expert Comment

    by:RuadRauFlessa
    oops sorry bout that was a bit hasety there.
    0
     
    LVL 3

    Expert Comment

    by:RuadRauFlessa
    Thanks jakekula
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    jakekula,
    it surprises me that you find that comment as the best suiting your needs.
    Can you give us some feedback please?
    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    Ach joh, laat toch .......
    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    Thanks for putting things right Venabili, the forum suffers from degeneration.
    ;JOOP!
    0

    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.

    Suggested Solutions

    Title # Comments Views Activity
    what is spring 3 29
    either24  challenge 19 58
    how to extract window origin in spring 2 39
    fix34  challenge 9 48
    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…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    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…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    884 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