|
Author |
Topic: linkedList? Hash table? (Read 509 times) |
|
sspboyd
|
linkedList? Hash table?
« on: Aug 13th, 2004, 8:35pm » |
|
Hi, I am bumping up against the limits of my programming knowledge again. I have been looking through some Java sites trying to find a way to handle some data I am working with in a sketch. I want to sort a list of colours by their prevalence in an image. Currently I am doing this by creating two separate arrays, one for the colour value, and another for the number of occurences of that colour. Then bubble sorting the occurences array and having to keep the colour array in step. Is it possible to keep both types of information in one data structure? Should I be looking at hash tables or linked lists, or something else? Thanks, steve
|
gmail.com w/ sspboyd username
|
|
|
TomC
|
Re: linkedList? Hash table?
« Reply #1 on: Aug 13th, 2004, 11:34pm » |
|
Once you have the colours and counts, try putting them both in an object which implements the Comparable interface. Then you can use the Collections.sort method, or the Arrays.sort method to sort a collection of these objects (either an array, or a Vector). e.g. Code: class ColourCounter implements Comparable { color col; int count; ColourCounter(color col, int count) { this.col = col; this.count = count; } public int compareTo(Object o) { // returns: // < 0 if this is less than o, // 0 if this is equal to o // > 0 if this is greater than o return count - ((ColourCounter)o).count; } } |
| I don't think a map (or hash table) will help here, although (ironically) for collecting the counts, a map of colors to counts might be appropriate.
|
« Last Edit: Aug 13th, 2004, 11:51pm by TomC » |
|
|
|
|
Euskadi
|
Re: linkedList? Hash table?
« Reply #2 on: Aug 14th, 2004, 5:37pm » |
|
also, linkedList doesn't work in Microsoft's virtual machine, so if you implement it only people with a VM that supports java 1.2 or higher will be able to see your stuff.
|
|
|
|
sspboyd
|
Re: linkedList? Hash table?
« Reply #3 on: Aug 16th, 2004, 8:19pm » |
|
on Aug 13th, 2004, 11:34pm, TomC wrote:Once you have the colours and counts, try putting them both in an object which implements the Comparable interface. |
| Excellent. Thanks Tom. I haven't given it a shot yet (at the cottage all weekend), but I've read up on using Comparable and I'll work on it today. on Aug 13th, 2004, 11:34pm, TomC wrote:I don't think a map (or hash table) will help here, although (ironically) for collecting the counts, a map of colors to counts might be appropriate. |
| I'll have to think about this and try to get it work.
|
gmail.com w/ sspboyd username
|
|
|
fry
|
Re: linkedList? Hash table?
« Reply #4 on: Aug 17th, 2004, 12:58am » |
|
to reiterate euskadi's point.. approximately 60% of people viewing applets on web pages are still using the microsoft vm, so if you use features like hashmap and comparable, your applet won't work on their machine.
|
|
|
|
sspboyd
|
Re: linkedList? Hash table?
« Reply #5 on: Aug 17th, 2004, 2:17am » |
|
on Aug 17th, 2004, 12:58am, fry wrote:if you use features like hashmap and comparable, your applet won't work on their machine. |
| ok. I'll keep that in mind for other projects. The project I am working on is for outputting prints so it only has to run on my mac for now.
|
gmail.com w/ sspboyd username
|
|
|
Euskadi
|
Re: linkedList? Hash table?
« Reply #6 on: Aug 22nd, 2004, 12:58am » |
|
does anyone know of efficient pseudo linked-list or hashtable classes that can be included in our sketches? i opted to use linked lists for speed but I would gladly run a Java 1.1 compatible version if I could do so without losing a lot of speed.
|
|
|
|
|