Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Array question

Thread view: 
bellsb@gmail.com - 25 Apr 2006 04:44 GMT
i'm trying to write a program that computes the totals from a
two-dimensional array then sorts by the largest total. however, I'm
loosing the original record when i do the sort, becuase i can't figure
out how to pass the index[key] when the swap takes place.

public class WeeklyHours {
 public static void main(String[] args) {

   //declare and define the array
   int[][] employeeHours = {
        {2, 4, 3, 4, 5, 8, 8},
        {7, 3, 4, 3, 3, 4, 4},
        {3, 3, 4, 3, 3, 2, 2},
        {9, 3, 4, 7, 3, 4, 1},
        {3, 5, 4, 3, 6, 3, 8},
        {3, 4, 4, 6, 3, 4, 4},
        {3, 7, 4, 8, 3, 8, 4},
        {6, 3, 5, 9, 2, 7, 9}};

    //display hours total hours worked by employee
    int[] totals = sumHours(employeeHours);

    int[] index = sort(totals);
    //for (int i = 0; i <= index.length; i++)
    //    System.out.println(index[i]);

    printArray(index, totals);

 }

 //define method which totals each row
 public static int[] sumHours(int[][] array) {
   //declare array and set values to 0
   int[] total = new int[array.length];

   //for loop used to tally array values
   for (int i = 0; i < array.length; i++){
        for (int j = 0; j < array[i].length; j++)
            total[i] += array[i][j];
        }

    return total;

 }

 //define method to sort array in desending order
 public static int[] sort(int[] array) {

     //setup array to keep track of employees
     int[] index = new int[array.length];
     for (int i = 0; i < array.length; i++)
              index[i] = i;

     //start for loop from bottom of array
     for (int i = array.length - 1; i >= 1; i--) {
         int currentMin = array[0];
         int currentMinIndex = 0;

         for (int j = 1; j <= i; j++) {
             if (currentMin > array[j]) {
                 currentMin = array[j];
                 currentMinIndex = j;
             }
         }

         if (currentMinIndex != i) {
             array[currentMinIndex] = array[i];
             array[i] = currentMin;

         }
     }
     return index;
 }

 public static void printArray(int[] array1, int[] array2) {
     for (int i = 0, j = 0; i < array1.length; i++, j++)
        System.out.println("Employee" + array1[j] + ": " + array2[i] + "
hours");
    }

}

I'd like this to output in this format:

employee7: 41
employee6: 37
employee5: 34
etc.
etc.

Any advice would be great, I think i'm close, just can't get the last
piece.

-sb
Roedy Green - 25 Apr 2006 04:49 GMT
>  //define method to sort array in desending order
>  public static int[] sort(int[] array) {

You can use a built in sort which you can trust . see
http://mindprod.com/jgloss/sort.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Bjorn Abelli - 25 Apr 2006 15:22 GMT
<bellsb@gmail.com> wrote...
> i'm trying to write a program that computes the totals from a
> two-dimensional array then sorts by the largest total. however,
> I'm loosing the original record when i do the sort, becuase
> i can't figure out how to pass the index[key] when the swap
> takes place.

There are several solution to this, so I'll give you the two that comes up
on the top of my head.

1 Swap the elements of the array index at
 the same time as you make the swap in totals.

 ...
 for (int i = array.length - 1; i >= 1; i--)
 {
    int currentMin = array[0];
    int currentMinIndex = 0;

    int currentIndex = 0; // <-

    for (int j = 1; j <= i; j++)
    {
       if (currentMin > array[j])
       {
          currentMin = array[j];
          currentMinIndex = j;

          currentIndex = index[j]; // <-
       }
    }

    if (currentMinIndex != i)
    {
       array[currentMinIndex] = array[i];
       array[i] = currentMin;

       index[currentIndex] = index[i]; // <-
       index[i] = currentIndex; // <-
    }
 }
 ...

2 [My preferred solution]
 Encapsulate the employees working hours as properties
 of an Employee class. Create an array (or another
 collection) of Employees which you sort by the
 total hours they've made...

// Bjorn A


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.