aspose file tools*
The moose likes Beginning Java and the fly likes How to find small value from Integer array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to find small value from Integer array?" Watch "How to find small value from Integer array?" New topic
Author

How to find small value from Integer array?

Kondal reddy
Greenhorn

Joined: Sep 23, 2003
Posts: 22
How can I found small value from my array?

like {20.30.40.45,50,80.90} from of this how can find 20 is small value in this array.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

- Initialize a variable M to Integer.MIN_VALUE
- Compare each element in the array to M
- If the element is smaller than M, set M equal to that element's value.

When you get to the end of the array, M is the minimum value in the array. If you want the index of the minimum value, then just keep a second variable to hold the index, and set it each time you set M.


[Jess in Action][AskingGoodQuestions]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

- Initialize a variable M to Integer.MIN_VALUE

Initializing it to the first element of the array would do be fine.
(A little check of the array size needed)

Setting M to Integer.MIN_VALUE, no other value could be smaller than that !


[My Blog]
All roads lead to JavaRanch
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Ah. I meant Integer.MAX_VALUE. Sorry.

You could use the first element, but then you have to check for the special case of zero-length arrays.
[ February 23, 2006: Message edited by: Ernest Friedman-Hill ]
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

Initializing it to the first element of the array would be fine.


Our usual college approach.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

don't you still need to check for 0 length arrays? i don't think returning Integer.MAX_VALUE is the correct answer if i have NO data


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ray Dutil
Greenhorn

Joined: Jan 11, 2006
Posts: 3
Wouldn't you be better off using some type of collection like an ArrayList or something? Or do you have to use a simple Array?


Life can only be understood backwards; but it must be lived forwards. - Kierkegaard
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
Or you can write a simple bubblesort and then just get the lowest value in element 0. It might actually be faster this way, even with a slower sort algorithm like bubblesort.

Using ArrayList defeats the purpose of learning how to program. Which is better?

A. Someone who learns Java and a good chunk of the API, might even get one or two of those certificates of dubious value.

or

B. Someone who learns how to program, understands the underlying contructs of library classes and computers themselves and can easily move to other languages and even write their own libraries and therefore can leverage existing libraries better then A.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find small value from Integer array?