Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-26
23
Medium Priority
?
414 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
Comment
Question by:jakekula
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3
  • +3
23 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 12408350
Arrays.sort(myArray);
int max = myArray[myArray.length-1]
int min = myArray[0];
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 1000 total points
ID: 12408352
By sorting it (using java.util.Arrays.sort()) and taking the first and the last.

0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12408952
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12408976
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
ID: 12408987
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
ID: 12409003
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
ID: 12409012
>> 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
ID: 12409094
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
ID: 12409102
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
ID: 12409119
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
ID: 12409145
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
ID: 12409180
> Then you don't need a loop

you still need the loop
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12409193
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
ID: 12409399
>> 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
ID: 12409407
> 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
ID: 12409552
oops sorry bout that was a bit hasety there.
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12409587
Thanks jakekula
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12409640
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
ID: 12413145
Ach joh, laat toch .......
0
 
LVL 24

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

636 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