Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

two questions

 
alireza massihzadeh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Soheil Tayari
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic