*
The moose likes Beginning Java and the fly likes two questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "two questions" Watch "two questions" New topic
Author

two questions

alireza massihzadeh
Greenhorn

Joined: Oct 26, 2008
Posts: 1
i need to create method, which traverse the collection of jobs, and find the most urgent one, and also if two have the same urgency, use the one earlier in the collection.

another one is that i have made my arraylist of jobs, but i need to write method to count how many jobs are in the arraylist.
[ October 26, 2008: Message edited by: alireza massihzadeh ]
Soheil Tayari
Greenhorn

Joined: Apr 04, 2008
Posts: 29
Take a look at Java API. It's your best friend.

http://java.sun.com/j2se/1.5.0/docs/api/

ArrayList has pretty much everything you need to do with a collection just take a look at the API.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Welcome to JavaRanch, Soheil Tayari
Another useful link is the Java Tutorials. There is actually a ready-made class which maintains priority; you can find it by going through the API under P for Priority.
If you have been told to use ArrayList, you can put a priority field in your Job class and create a Comparator to match and use that with a sorting algorithm (there is one mentioned in the Java Tutorial somewhere). Since the algorithm used is "stable" you can sort by priority and two elements with the same priority will retain insertion order.
If you have been told to write your own sort algorithm, you will haev to do that as well.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

For the first question, you may be better off using a some Sorting behavior (see The FAQ, question 24 for some resources on sorting. If this is for homework, best to start there.

If this is not homework then you minds-well use either the sorting mechanisms provided by the Collections API, or use one of the sorted collections to add the jobs to so you don't have to worry about iterating/sorting each retrieval point. For example the TreeSet with a Comparator to order based on urgency and time-created.


Steve
Soheil Tayari
Greenhorn

Joined: Apr 04, 2008
Posts: 29
Originally posted by Campbell Ritchie:
Welcome to JavaRanch, Soheil Tayari


Seems you're chasing me for editing my posts .
Just kidding, thank you by the way.
I again apologize for bad-posting in the previous thread CR.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Originally posted by Soheil Tayari:


Seems you're chasing me for editing my posts .
Just kidding, thank you by the way.
I again apologize for bad-posting in the previous thread CR.


No I'm not, but I am responsible (along with Marc and Maneesh) for this forum. And we accept the apology.
 
Don't get me started about those stupid light bulbs.
 
subject: two questions