• Post Reply Bookmark Topic Watch Topic
  • New Topic

min max array list  RSS feed

 
eddie pa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all, sorry to bother you with this but i'm trying to find the smallest/largest int in an abstract data type list. what's the proper way to write min/max test - i see so many different ways online.
What I'm thinking is:


int smallest = Integer.MIN_VALUE;
int largest = Integer.MAX_VALUE;

}
if (x < y)
{

System.out.println("Smallest");

your insight is much appreciated.

- eddie
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post a SSCCE or may be the code which you have been trying for the same.
One way is to iterate through the list and find out the maximum and minimum elements.
 
eddie pa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, thanks for your reply, the code is very long and complicated. i guess my question should be, how to write a min max code in java? i'm new to java so i'm not sure if there is a standard way to get min max value?
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can focus on solving the min-max problem, so the way to go would be to write a sample program to solve that problem and then integrate with your large problem.
you can explore the List, Collection api. you can create a List of the elements and then sort it. If you sort it in descending order than you can pick the first element for max and the last element for min.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And before you write a single line of java code, figure out how YOU would do it, if all you had was a pencil, paper, and a VERY short term memory.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!