• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

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()
0
jakekula
Asked:
jakekula
  • 7
  • 5
  • 3
  • +3
2 Solutions
 
objectsCommented:
Arrays.sort(myArray);
int max = myArray[myArray.length-1]
int min = myArray[0];
0
 
zzynxSoftware 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 7
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now