Results 1 to 10 of 10
  1. #1
    Tartaglia is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Average of an ArrayList filled with Objects

    Hello everyone,

    I have an arrayList filled with Objects ('Worker') that have three characteristics ('id', 'humanCapital', 'wage') and I am trying to compute the average of one of the characteristics, say 'wage'. This is is what I have so far, my class 'Worker' which countains the constructor:

    Java Code:
    import java.util.Comparator;
    import java.util.ArrayList;
    
    
    public class Worker {
    int id;
    double humanCapital;
    double wage;
    double random;
    	
    public Worker (int i, double h, double w){
    id = i;
    humanCapital = h;
    wage = w;
    		}
    }
    And my main class 'exampleList2':

    Java Code:
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class exampleLists2{
    
    public static void main(String args[]){
    												
    ArrayList workerList = new ArrayList();
    //List<Worker> workerList = new ArrayList<Worker>();
    
    workerList.add(new Worker(10,20,30));
    workerList.add(new Worker(11,21,31));
    workerList.add(new Worker(12,22,32));
    		   			
    		    
    ArrayList<Double> workerList3 = new ArrayList<Double>();
    		    
    			
    for(int i=0; i < workerList.size(); i++){
    Worker aWorker = (Worker) workerList.get(i);
    System.out.println("The Worker wage is " + aWorker.wage);
    workerList3.add(aWorker.wage);
    				
    }				
    			
    for(int i=0; i < workerList3.size(); i++){ 
    double sum = workerList3.get(i); 
    System.out.println("The sum is " + sum);
    				}
    }
    }
    The last loop is where I am having difficulties accessing the characteristics, I cannot type :
    'double sum = workerList3.get(i).wage;' for instance. Does anyone have an idea how I could proceed.

    Thank you
    Tartaglia

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Average of an ArrayList filled with Objects

    Since you know about generics (you have an ArrayList<Double>), then workerList should surely be an ArrayList<Worker>. It would save on the casting later on.

    Anyway, what is the point of the workerList3?
    Why not simply sum the wages as you loop over the workerList?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Tartaglia is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Average of an ArrayList filled with Objects

    You are right about the generics, I wasn't familiar with their purpose which is why I had commented that line. Now, I uncommented line 10 (and commented line 9) and took the casting out in line 21.

    I am not exactly sure how to sum the wages in the loop over the workerList, this is what I got so far (I was hoping to use 'double sum2 += sum[i];' but I am getting an error when doing so):

    Java Code:
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
     
    public class exampleLists2{
     
    public static void main(String args[]){
                                                     
    //ArrayList workerList = new ArrayList();
    List<Worker> workerList = new ArrayList<Worker>();
     
    workerList.add(new Worker(10,20,30));
    workerList.add(new Worker(11,21,31));
    workerList.add(new Worker(12,22,32));
                             
    
    double sum[] = new double[workerList.size()];
                 
    for(int i=0; i < workerList.size(); i++){
    
    sum[i] = workerList.get(i).wage;
    double sum2 = sum[i]; 
    System.out.println("The Worker wage sum is " + sum2);
                     
    }    
    }
    }
    Last edited by Tartaglia; 05-14-2012 at 12:50 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Average of an ArrayList filled with Objects

    You just need a single 'sum' variable, which would be a double.
    Then sum += wage.

    All you;re doing at the moment is creating an array of the wages.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Tartaglia is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Average of an ArrayList filled with Objects

    I have tried the following, but I am getting an error:

    Java Code:
    double sum[] = new double[workerList.size()];
                 
    for(int i=0; i < workerList.size(); i++){
    double sum3 += workerList.get(i).wage; //"Syntax error on token "+=", = expected"
    double sum2 = sum[i]; 
    System.out.println("The Worker wage sum is " + sum);
                    
    }
    Last edited by Tartaglia; 05-14-2012 at 03:51 PM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Average of an ArrayList filled with Objects

    As Tolls already told you, you don't need an array.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Tartaglia is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Average of an ArrayList filled with Objects

    But how do I manage to add up all the respective wages in 'sum3' as I go through the loop? Maybe something like this?
    Java Code:
                
    for(int i=0; i < workerList.size(); i++){
    
    double sum3 = workerList.get(i).wage+workerList.get(i-(i-1)).wage;
    
    System.out.println("The Worker wage sum is " + sum3);
                     
    }

  8. #8
    Tartaglia is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Average of an ArrayList filled with Objects

    I just tried to declare the variable 'sum3' outside the loop and then it suddenly worked (I also had to initialize as 0). What exactly is the difference between declaring it inside/outside the loop?

    Java Code:
    double sum3 = 0;
    for(int i=0; i < workerList.size(); i++){
    sum3 += workerList.get(i).wage;
    //double sum2 = sum[i]; 
    System.out.println("The Worker wage sum is " + sum3);
                    }
    Thanks so much for your help!!

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

    Default Re: Average of an ArrayList filled with Objects

    If you declare that variable inside your loop, you're declaring it over and over again (everytime your loop body executes) and maybe even more important: it doesn't exist anymore when your loop has finished.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tartaglia is offline Member
    Join Date
    May 2012
    Posts
    14
    Rep Power
    0

    Default Re: Average of an ArrayList filled with Objects

    Oh yes of course, I did not think of that at all.

    Well, thank you guys for your help, I really appreciate it!!!

Similar Threads

  1. Add objects to ArrayList with Polymorphism
    By africanhacker in forum New To Java
    Replies: 7
    Last Post: 06-29-2011, 03:59 PM
  2. [HELP] Removing objects from an ArrayList!
    By bpx95 in forum New To Java
    Replies: 4
    Last Post: 04-30-2011, 06:54 AM
  3. ArrayList with Objects the right approach?
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 10:26 AM
  4. Help with objects in ArrayList
    By BiteMuncher in forum New To Java
    Replies: 7
    Last Post: 02-21-2011, 12:55 PM
  5. ArrayList with different objects? Help
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 09:51 PM

Posting Permissions

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