Results 1 to 8 of 8
  1. #1
    Pyrexkidd is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default 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.

    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]);
    }
    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.
    then i need to sort by hours and do the same thing. normally I would use:
    Java Code:
    Arrays.sort(names);
    but this doesn't sort the other two arrays and I'm stuck with my names not matching pay rate etc.

    Any suggestions please?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  4. #4
    Pyrexkidd is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    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:
    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
    }
    so how would I sort my array of employees?

    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:
    Java Code:
    ...
    local_name = castList[1].getName;
    ...
    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?

  5. #5
    Pyrexkidd is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    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;
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Pyrexkidd View Post
    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?
    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

  7. #7
    Pyrexkidd is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    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
    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...

    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.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Pyrexkidd View Post
    I can create objects using that class. but I'm not sure how to sort an array of objects.
    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

  1. How to create parallel arrays
    By Roselicious in forum New To Java
    Replies: 6
    Last Post: 04-18-2010, 01:10 PM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  3. Sorting Two Arrays
    By Faye Rett in forum New To Java
    Replies: 4
    Last Post: 03-07-2010, 02:00 AM
  4. I need examples using parallel arrays
    By dangerzone9k in forum New To Java
    Replies: 10
    Last Post: 04-04-2009, 05:11 PM
  5. [SOLVED] Parallel Arrays with Choice ComboBox - need assistance
    By Judoon_Platoon in forum Java Applets
    Replies: 14
    Last Post: 10-01-2008, 10:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •