Results 1 to 8 of 8
Thread: Sorting Multiple Parallel Arrays
- 05-10-2010, 12:09 AM #1
Member
- Join Date
- May 2010
- Posts
- 11
- Rep Power
- 0
Sorting Multiple Parallel Arrays
So it's end of the year finals time... My EOY the project is to input a file, do a bunch of calculations, sort redo the calculations, or I could do the calculations once and then sort the calculations along with the input. I think since I'm reading the file into an array anyway it's easy enough to print out line by line and do the calculations after each sort.
Ok so it's easy to iterate through the parallel arrays and display the results.
Where I am getting stuck is in the sort and print. ie I need to sort an alphabetical list of names and then print out the results sorted.Java Code:for ( i =0 ; i < names.length ; i++){ outFile.print(names[i] + " " + hours[i] + " " + payRate[i] + " "); outFile.print(calcPay(hours[i], payRate[i]) + " " + calcTax(payRate[i]); }
then i need to sort by hours and do the same thing. normally I would use:
but this doesn't sort the other two arrays and I'm stuck with my names not matching pay rate etc.Java Code:Arrays.sort(names);
Any suggestions please?
-
You'd be better off not using parallel arrays, but rather create a class that holds all the information about a single person (name, pay, etc), and create objects for each person (employee?). If you want to make it even better, have the class implement the Comparable interface so that sorting an array of these objects is much easier.
- 05-10-2010, 06:47 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
If you simply must have your information in parallel arrays (Fubarables solution is more practical, and more in tune with OOP), then you'd need to write your own sorting algorithm (not very difficult), and do the same transformations on both arrays at the same time. An example with bubble sort:
Java Code:int[] values = {...}; String[] names = {...}; void bubble() { for(int i = 0; i < values.length; i++) { for(int j = i; j < values.length; j++) { if(values[i] > values[j]) { swapValues(i, j); swapNames(i, j); } } } } void swapValues(int i, int j) { int temp = values[i]; values[i] = values[j]; values[j] = temp; } void swapNames(int i, int j) { String temp = names[i]; names[i] = names[j]; names[j] = temp; }Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-11-2010, 06:24 AM #4
Member
- Join Date
- May 2010
- Posts
- 11
- Rep Power
- 0
while I agree with you entirely that's not the assignment. this is the teacher who wrote in the notes on my first assignment (it was a add two numbers from the command line type program, to which I used an array list and allowed the addition of multiple numbers) "You don't know how to use this. DO the assignment and turn in again."
however, I am curious about this "Comparable" interface. how would that be implemented? personally I would do something like:
so how would I sort my array of employees?Java Code:public class employee{ private string _name; private double _hoursWorked; private double _payRate; public static void employee(string name, double hours, double pay){ _name = name; _hoursWorked = hours; _payRate = pay; } public static string getName(){ return _name; } public static double getHours(){ return _hoursWorked; } public static double getPay(){ return _payRate; } } //end employee public class final_project{ public static void main(){ ... Employee[] castList = new Employee[X]; for ( int i = 0; i < castList.length; i++){ line = Filereader(INFILE); StringTokenizer st = new StringTokenizer(line); name = st.NextToken(); pay = st.NextToken(); hours = st.NextToken(); castList[i] = (name, pay, hours); } //end for }
this is what I would think would be the best implementation. an array of a class.
then I could get any values I needed with:
also can any method inside my final_project {} class access my employee{} class via the public methods? or do I have to pass my array of ojects?Java Code:... local_name = castList[1].getName; ...
- 05-11-2010, 07:37 AM #5
Member
- Join Date
- May 2010
- Posts
- 11
- Rep Power
- 0
so here is my take on the parallel swap with my modification for a 2d data array instead of a 1d array. also I passed the array into the method so the method could modify the array. otherwise the code wouldn't compile.
any thoughts?
Java Code:int[][2] values = {...}; String[] names = {...}; void bubble() { for(int i = 0; i < values.length; i++) { for(int j = i; j < values.length; j++) { if(values[i][1] > values[j][1]) { swapValues(i, j, values); swapNames(i, j, names); } } } } void swapValues(int i, int j, int[][] values) { int temp = values[i][1]; values[i][1] = values[j][1]; values[j][1] = temp; int temp2 = values[i][2]; values[i][2] = values[j][2]; values[j][2] = temp; } void swapNames(int i, int j, string[] names) { String temp = names[i]; names[i] = names[j]; names[j] = temp; }
- 05-11-2010, 07:55 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-12-2010, 03:01 AM #7
Member
- Join Date
- May 2010
- Posts
- 11
- Rep Power
- 0
haha... shoulda seen that one coming... it's only funny cause it got me stuck for close to five minutes... if I've made that mistake once...Yep, as soon as you run that code you'll get an ArrayIndexOutOfBoundsException. Your values[][] array has two columns. Array indexing starts at zero so the column indexes are 0 and 1.
kind regards,
Jos
but how about implementing this code with objects rather than parallel arrays?
I can create objects using that class. but I'm not sure how to sort an array of objects.
- 05-12-2010, 06:34 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Suppose you have two objects, both instantiations of a same class T; also suppose you can say: a.comparteTo(b) and the result is negative if the relation a < b is true; the result is positive if the relation a > b is true and the result is zero if a == b is true. That is exaclty what the Comparable interface is about and it is possible to sort a list of Comparables because the relation defines an ordering. All you have to do is implement that interface and apply one of the ready made sorting algorithms (Collections, Arrays).
kind regards,
Jos
Similar Threads
-
How to create parallel arrays
By Roselicious in forum New To JavaReplies: 6Last Post: 04-18-2010, 12:10 PM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Sorting Two Arrays
By Faye Rett in forum New To JavaReplies: 4Last Post: 03-07-2010, 01:00 AM -
I need examples using parallel arrays
By dangerzone9k in forum New To JavaReplies: 10Last Post: 04-04-2009, 04:11 PM -
[SOLVED] Parallel Arrays with Choice ComboBox - need assistance
By Judoon_Platoon in forum Java AppletsReplies: 14Last Post: 10-01-2008, 09:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks