run into implementation problem with game (java)
Hi
I have an arraylist which contains card objects. Each object has a method which will return its value. I want to sort the array in numerical order by object value. I also need to remove and add objects to the list. Ideally I would like a structure which would order the objects as they are added to the list. I'm still at a point where I could replace the arraylist with a different structure. Can anyone suggest a good technique to solve this problem ? And can anyone point me in the right direction for a tutorial which goes through the use of data structures such as sortedLists, arrayLists, maps etc Thanks |
All all you need to do is have the objects implement the Comparable interface the way you want properly. Then you can just use the static Collections.sort() on the ArrayList.
For more info, look into the Java Collections Framework: http://java.sun.com/docs/books/tutorial/collections/ Don't forget http://java.sun.com/api |
if you wanted to sort as you added each element you could create a class (say SortedArraylist) which extends ArrayList, then you can add a new method (or override the old one) to sort the list after adding each card using aaa's solution.
Code:
public class SortedArrayList extends ArrayList |
There is also the SortedSet interface. Any class that implements this (like TreeSet) will automatically sort it's objects. The problem is that it's a Set, and there can be no two or more identical objects in a Set.
ArrayList implements the List interface. So, it is a good idea to do this: List list = new ArrayList(); By doing this, you can switch to any other List on a whim (performance reasons, etc), without changing much code (perhaps just one line): List list = new Vector(); You can switch to using a Vector easily, without changing lots of code. You may not be able to do this if you extend ArrayList and add a new method not in the List interface. If you do extend it, I suggest that you override one (or more) of the methods already in List, to retain compatibility. |
As said a TreeSet would be ideal with a Comparator if you are dealing with only one pack of cards in your game - as each card would be unique - You couldn't just sort them by value (at least I don't think you'd want to ?) as there would be four duplicate values for each card. Of course, we don't know what the rules of your game will be :)
|
Thanks for all the advice - you people are the best :)
Sorry i neglected to say that at most the array could contain 4 duplicate values. The value returned is between 2-14 representing the numerical value of the card, this is going to mean i can't use some structures, such as the sets. The game involves numerical comparison of cards, suite value is unimportant. If the cards are sorted it is easier for me to write the code that the comp uses to pick a good hand or to select a card to play. Each card could return a unique id, i could associate the id to the numerical value using a hash table. This would solve the sorting problem with the sets (if i sort by the id) but ideally i'd like to find a sorting solution without having to do this. Im going to do some reading about the comparable interface. Feel free to throw in a few more suggestions now you know about the duplicates. Thanks all |
Hi again
I've written a method which sorts the arraylist as i need but i can't figure out how i can have the list sorted as i add objects to it. This is what i've come up with. (Card object implements Comparable) Code:
public int compareTo(Object test){ Code:
public boolean add(Comparator cardToSort) Code:
//order the cards in reverse numerical order. |
Can't say for certain but seeing as I don't know exactly what the orginal code in the overriden add method is (could probably search and find it), when you call add you are calling your own created add, it's probably easier just to create another method that uses the orginal add, something like this (assuming your card class is called Card), then just use myDeckOfCards.insert(a_card):
Code:
|
Uhmm, well, just realised that was probably my worst and inaccurate explanation of anything yet on these boards :) After I actually thought about it and remembered the principles of Object Orientated Programming - this:
Code:
|
Thanks looking_lost your last post clears up any problems. Its all working fine :)
Thanks to everyone else as well |
All times are GMT -5. The time now is 05:50 PM. |