• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Find smallest element in arraylist

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, could anyone here please explain to me how to search an arraylist and return the smallest element from it? I know I need to search through the arraylist and do a compareto on each element, but not sure how to write it.
[ March 06, 2007: Message edited by: big momma ]
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

first of all: Take a look at the naming policy here on the ranch
( http://www.javaranch.com/name.jsp ). Baiscly you need a first and a
second name..

You need to do the following:


Did that help ? if not, where are you stuck in your implementation?

/Svend Rost
 
Gary Goldsmith
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize about the naming problem (I think ive fixed it now).

As for my problem I 'think' I know how to do it algorithmically:
Get the first element in the array compare to the next one, if its smaller than the first one then make that 'i' and then compare that one to the next one in the array, and so forth until the smallest element is found which I can then pass to a method. But I'm a bit lost with the actual code.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you show your attempt at coding it first and where your stuck. Or if you can't get code working, at least show pseudocode. You'll get much greater benefit to struggle with the problem first before seeing a solution.
 
Gary Goldsmith
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Gary Goldsmith
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe something like this would be better?

 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

okay, so you know which steps to take when you have to solve this problem
but you have problems with the implementation.. let's take at what you wrote:


- Let's use j as the variable that contains the current lowest value. We need
to init it with a large value, say 10000 (you can take a look at the Interger, http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Integer.html ,
class for a nicer way to initialize j with another value than 10000).

- Your for-loop condition looks good, if queue is an array list you gets
its size with the .size() method, and if queue is an array you gets its
length with the instane variable 'length'

- You need to remove an element from the arraylist/array. This is done by
queue[index] for an array, and you can look here: http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
for an approapriate method for the array list.

- The idea behind your comparision is also correct.. lets make it abit more
concrete:


Try to implement the above and post your results.. I'll check back later.

Happy coding

/Svend Rost
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm, we had a John Smith before ... is he gone? Does the ranch care about duplicate names?
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
Hmmm, we had a John Smith before ... is he gone? Does the ranch care about duplicate names?


Although I havn't seen an official rule on this, this queryin the JavaRanch
forum gives the impression, that "no, the ranch doesn't".

/Svend Rost
 
Gary Goldsmith
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there again, well i tried doing the array part and couldn't get it, so I carried on with other things, but now I'm trying again.


this I know gets me the last element in the arraylist.
test1.getLength() will get me the size of all the objects, I want to get the first element myArray.get(0) and then compare it to the next element.

ArrayList: 3, 5, 2, 9

I want to return the smallest element.

A pseudo style code answer would help very much
[ March 13, 2007: Message edited by: big momma ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Svend Rost gave a very appropriate pseudo-code example in his first post, was there a particular part of it that you didn't understand. Personally, I usually initialize the current_lowest_value to the first element in the collection, and then compare all the other elements to the current_lowest_value, updating it as necessary, but that is a trivial implementation detail.
[ March 13, 2007: Message edited by: Garrett Rowe ]
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,

to solve the problem of finding the smallest value in an array list you need
to solve the following steps (it is always a good idea to break your problems
into smaller steps, and them one by one):
- loop through an arraylist

- remove an element in the arraylist

- compare two of the elements in the list

- save the smallest of those two values

Lets make a skeleton system:



Try to use the above.. and read my previous post.

/Svend Rost, who'll check back later
 
Gary Goldsmith
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats been very helpful, thank you. I think i'm almost there. I shall work on it more tonight. So for now thank you, I will come back tomorrow with good or bad news
 
Hey cool! They got a blimp! But I have a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic