We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Array/hashmap. (Read 1006 times)
Array/hashmap.
Feb 23rd, 2010, 3:48pm
 
I've just bout got my head around 2D/3D arrays, and am currently using a 2D array ([array index] "[array]") that looks like this:

[1] "place              num1 num2 num3 num4"
[2] "Canada      203      181      112      496"
[3] "USSR        281      111      103      495"
[4] "Norway      134      182      112      428"
etc..

I'm wanting to be able to re-order the 2d array in order of ascending 'num1' values etc. I've had a quick look around for tips on sorting, but it seems like I might need to learn to use hashMap first and using that instead of my arrays? - would this mean re-writing everything I've already go working from my arrays?
Re: Array/hashmap.
Reply #1 - Feb 23rd, 2010, 9:47pm
 
Hey, check out "Sort" here:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html
Re: Array/hashmap.
Reply #2 - Feb 24th, 2010, 5:23am
 
Im not really sure what to take from that:/ - this seemes to sound like what I want;

public static void sort(int[] a,
                       int fromIndex,
                       int toIndex)

but I couldnt work out if it was saying it would sort a section of an array into order, or if it would sort the whole array by comparing a selection of each string.. I think im after the latter as I would need to loop through my array, then compare the last value of each array.
How do I go between the reference listed methods etc to 'actual code'..? I dont really understand much of the terminology used in references Undecided Is it as simple as just using the syntax sort(array[], int, int) inside a normal processing sort() function?
Re: Array/hashmap.
Reply #3 - Feb 24th, 2010, 6:31am
 
What you ask for is not trivial, because the comparator has to extract the number from the string and to convert it to number each time two elements of the array are compared. Which would be a bit slow too.
Is there any reason to keep these strings in string form

Now, it isn't unsurmountable, but Java compares strings like any object, in a generic way, so it doesn't offer a mean to automatically compare a range in a string. You have to do that yourself, using the sort(T[] a, Comparator< super T> c) form.
Ie. you have to write a Comparator class and use it in the sort call.
Yes, it might be a bit intimidating but not as hard as it seems. Or is it

I really should write the tutorial on comparators and comparable objects I plan to do for so long...
Re: Array/hashmap.
Reply #4 - Feb 24th, 2010, 6:45am
 
PhiLho  wrote on Feb 24th, 2010, 6:31am:
Is there any reason to keep these strings in string form

Not really, but the arrays which are sitting in the rows of the main array have both strings and ints, so I would have to take the strings out and make a new array for them too. I thought it would be tidier to use a 2d array, but perhaps I'll just split everything into 5 individual ones. Would 5 int arrays be speedier/slower than the current 2d string array

Quote:
Ie. you have to write a Comparator class and use it in the sort call.
Yes, it might be a bit intimidating but not as hard as it seems. Or is it


Im still learning about what a class is at the moment, perhaps it's just me that's struggling, it's just because I'm teaching myself Processing as a first coding language, so I've never had anyone explain to me how to go about constructing code properly. I for one appreciate every scrap of help in this I can find so I dont think a little tutorial would be time wasted!
Re: Array/hashmap.
Reply #5 - Feb 24th, 2010, 6:50am
 
Look at What uses less memory thread, user had a very similar issue.
You can make a very simple class, actually acting as data holding, with the advantage to name fields and specify their type while allowing to sort an array of these objects, keeping related information together.
Re: Array/hashmap.
Reply #6 - Feb 26th, 2010, 7:55am
 
Ok, so I've split much of my array bits'n'pieces into seperate 1D arrays, which might make it easier, if I have two (or more) arrays, and I use sort() to order one alphabetically/numerically, is there a trick to re-order the other array in the same way? eg

String[] words = {"five, three, four, two, one"};
int[] nums = {"5, 3, 4, 2, 1"};

if I sort 'nums' numerically to: 1, 2, 3, 4, 5, how might I apply this same ordering to 'words' to make it appear as one, two, three, four, five? - I dont want to actually sort the words array, just re-order it (by [i]) the same was the 'nums' array. L.
Re: Array/hashmap.
Reply #7 - Feb 26th, 2010, 9:51am
 
Ladle wrote on Feb 26th, 2010, 7:55am:
I've split much of my array bits'n'pieces into seperate 1D arrays, [...] is there a trick to re-order the other array in the same way

Yes. As shown in the thread I point to, drop this solution (separate arrays) and make only one array of objects grouping related data together, thus you are sure they are kept together...
Page Index Toggle Pages: 1