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

# 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
• 7
• 5
• 3
• +3
2 Solutions

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

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

0

Commented:
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];

0

Commented:
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

Commented:
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

Software 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

Software 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

Commented:
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

Commented:
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

Commented:
you could also use the following in your loop:

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

Commented:
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

Commented:
> Then you don't need a loop

you still need the loop
0

Commented:
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

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

Commented:
> 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

Commented:
oops sorry bout that was a bit hasety there.
0

Commented:
Thanks jakekula
0

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

Commented:
Ach joh, laat toch .......
0

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