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

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()
jakekulaAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
Arrays.sort(myArray);
int max = myArray[myArray.length-1]
int min = myArray[0];
0
 
zzynxConnect With a Mentor Software engineerCommented:
By sorting it (using java.util.Arrays.sort()) and taking the first and the last.

0
 
NaeemgCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
RuadRauFlessaCommented:
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
 
RuadRauFlessaCommented:
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
 
zzynxSoftware engineerCommented:
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
 
zzynxSoftware engineerCommented:
>> 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
 
RuadRauFlessaCommented:
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
 
RuadRauFlessaCommented:
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
 
objectsCommented:
you could also use the following in your loop:

hi = Math.max(hi, myArray[i]);
lo = Math.min(lo, myArray[i]);
0
 
RuadRauFlessaCommented:
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
 
objectsCommented:
> Then you don't need a loop

you still need the loop
0
 
sciuriwareCommented:
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
 
zzynxSoftware engineerCommented:
>> Then you don't need a loop
Of course you need one. What is the variable "i" in your code RuadRauFlessa?
0
 
TimYatesCommented:
> 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
 
RuadRauFlessaCommented:
oops sorry bout that was a bit hasety there.
0
 
RuadRauFlessaCommented:
Thanks jakekula
0
 
zzynxSoftware engineerCommented:
jakekula,
it surprises me that you find that comment as the best suiting your needs.
Can you give us some feedback please?
0
 
sciuriwareCommented:
Ach joh, laat toch .......
0
 
sciuriwareCommented:
Thanks for putting things right Venabili, the forum suffers from degeneration.
;JOOP!
0
All Courses

From novice to tech pro — start learning today.