participate


New To Java - Extending ArrayList Object - Help Needed!
<<   Back to Forum  |   Give us Feedback
This topic has 10 replies on 1 page.
stew123dai
Posts:2
Registered: 11/3/09
Extending ArrayList Object - Help Needed!   
Nov 3, 2009 6:31 AM

 
Hi Guys , I am rather new to Java and I am trying to extend the ArrayList object to that when an ArrayList of Integers exists it has a function to return the Average value of the Integers within the ArrayList. Below is the code I have so far:

import java.util.*;*
*import java.lang.*;
public class MathArrayList<Integer> extends ArrayList<Integer> {
 
double average;
 
public double Average() {
CalculateAverage();
 
return average;
}
 
private void CalculateAverage() {
double total = 0;
 
for(int i = 0; i < size(); i++) { 
 
total += get(i).intValue();
}
 
average = total / size();
}
}
 


When I attempt to compile this code I get: Cannot find symbol - Line 18 - ".intValue()" and I cannot work out why! I have created a smaller java project to test that I am using the method '.intValue()' correctly and it is converting and Integer to an int fine! Why cannot it not find a symbol?

Any help/comments if much appricated!

Thanks, Stewart
 
dcminter
Posts:9,319
Registered: 3/4/99
Re: Extending ArrayList Object - Help Needed!   
Nov 3, 2009 6:55 AM (reply 1 of 10)  (In reply to original post )

 
get returns an Object not the parameterized type. Use an iterator instead.
 
dcminter
Posts:9,319
Registered: 3/4/99
Re: Extending ArrayList Object - Help Needed!   
Nov 3, 2009 7:00 AM (reply 2 of 10)  (In reply to #1 )

 
One approach:
// Method names should not start with capital letters
private void calculateAverage() {
   double total = 0;
   for( final Integer value : this ) {
      total += value; // You can autounbox if you're confident the List will never contain nulls.
   }
   average = total / size();
} 

My inclination would be to implement this as an external function however:
public static double calculateCollectionAverage(final Collection<Integer> collection) {
   double total = 0;
   for( final Integer value : collection ) {
      total += value;
   }
   return (total / collection.size());
}


You should note that none of these approaches is threadsafe though.

Edited by: dcminter on 03-Nov-2009 15:00
 
warnerja
Posts:23,517
Registered: 9/26/01
Re: Extending ArrayList Object - Help Needed!   
Nov 3, 2009 7:04 AM (reply 3 of 10)  (In reply to #1 )

 
dcminter wrote:
get returns an Object not the parameterized type. Use an iterator instead.
Eh? ArrayList<E>'s get method is:
E get(int index);
So if E is Integer, get returns Integer, right?

@OP: I don't believe that's the actual code you're trying to compile.

If it is, there are some other issues with it.

#1: There's no reason to extend ArrayList for this. You aren't creating a special kind of ArrayList - you should just use an ArrayList and create a method that operates on that ArrayList to return the average. Favor composition over inheritance.

#2: Your calculate average method is not thread safe. You're calculating a member variable and then returning it, leaving room for another thread to alter that member variable in-between and get the wrong result. It should just calculate and return the average by using a local method variable. No need for the member variable there.
 
georgemc
Posts:16,804
Registered: 5/8/06
Re: Extending ArrayList Object - Help Needed!   
Nov 3, 2009 7:11 AM (reply 4 of 10)  (In reply to #1 )

 
dcminter wrote:
get returns an Object not the parameterized type.

It does?

// from java.util.List
 E get(int index);
 
// from java.util.ArrayList
public E get(int index) {
	RangeCheck(index);
 
	return elementData[index];
}


I wonder if you're thinking of Map.get() which does indeed accept Object rather than the parameterized type.
 
georgemc
Posts:16,804
Registered: 5/8/06
Re: Extending ArrayList Object - Help Needed!   
Nov 3, 2009 7:10 AM (reply 5 of 10)  (In reply to #3 )

 
warnerja wrote:
@OP: I don't believe that's the actual code you're trying to compile.

+1
 
dcminter
Posts:9,319
Registered: 3/4/99
Re: Extending ArrayList Object - Help Needed!   
Nov 3, 2009 7:11 AM (reply 6 of 10)  (In reply to #3 )

 
So if E is Integer, get returns Integer, right?

Yes. God knows what I was thinking... insufficient caffeine.
 
DeltaGeek
Posts:1,192
Registered: 11/21/07
Re: Extending ArrayList Object - Help Needed!   
Nov 3, 2009 7:57 AM (reply 7 of 10)  (In reply to original post )

 
Since no one else has said it, I would like to point out that the code

public class MathArrayList<Integer> extends ArrayList<Integer> { ... }

defines a placeholder type named "Integer" that has absolutely no relation to java.lang.Integer
 
jverd
Posts:51,846
Registered: 3/30/99
Re: Extending ArrayList Object - Help Needed!   
Nov 3, 2009 8:14 AM (reply 8 of 10)  (In reply to original post )

 
stew123dai wrote:
Hi Guys , I am rather new to Java and I am trying to extend the ArrayList object to that when an ArrayList of Integers exists it has a function to return the Average value of the Integers

I strongly advise against that. A List's job is to maintain its elements in the order they're added. Adding responsibilities like computing the average is not good OO.
 
stew123dai
Posts:2
Registered: 11/3/09
Re: Extending ArrayList Object - Help Needed!   
Nov 3, 2009 10:43 AM (reply 9 of 10)  (In reply to #7 )

 
Thanks for all the responses guys, I have tried using your code dcminter and it gives me the same error I was getting before I tried using the '.intValue' method. That is that the '+' operator cannot be applied to Integer. I have researched this before and am sure this is because an object Integer is returned and not an int. I wanted to created what I called a MathArrayList as i wanted to add more functions such as StandardDev. extra to the list of methods so i could use this new ArrayList within many of the project I have to create. The import java.lang.*; is only in there as I was trying to work out why the method could not be found. I still haven't found a direct solution to my problem and still need an object which can hold 'int's.


After reading all of the comment I have decided to try and complete this project in a different way. Thanks very much for all the help !

Edited by: stew123dai on Nov 3, 2009 10:41 AM
 
dcminter
Posts:9,319
Registered: 3/4/99
Re: Extending ArrayList Object - Help Needed!   
Nov 4, 2009 2:17 AM (reply 10 of 10)  (In reply to #9 )

 
That is that the '+' operator cannot be applied to Integer. I have researched this before and am sure this is because an object Integer is returned and not an int.

In that case you're running an old version of Java (or of the JDK at least). Generics and auto(un)boxing weren't introduced until Java 5. Upgrade.
 
This topic has 10 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums