File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find small value from Integer array?

 
Kondal reddy
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- 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.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- 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 !
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


Our usual college approach.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ray Dutil
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Robert Hill
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic