Welcome to Dream.In.Code
Become a Java Expert!

Join 150,199 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,988 people online right now. Registration is fast and FREE... Join Now!




Collection of doubles

 
Reply to this topicStart new topic

Collection of doubles, Ehh....

anorman
23 Sep, 2008 - 05:54 PM
Post #1

New D.I.C Head
*

Joined: 30 Jan, 2008
Posts: 14

Here's part of the code. Im not sure if using the array list is the like that is the correct way for constructing an empty list of doubles but it seemed the most logical. However in my tests the size does not work. I just want to know if how I'm going about this is correct before I go any futher. Thanks!

CODE
import java.util.ArrayList;
import java.util.Collections;

public class DescriptiveStatistics {
    
     ArrayList<Double> collection = new ArrayList<Double>();//Is this right?
    
    private int size;
    private double max;
    private double min;
    
    /**
     * Construct an empty collection of doubles that can store up to any
     * number of floating point numbers as long as the computer has memory.
     */
    public DescriptiveStatistics(){
        
    collection = null;    
    }
    
    /**
     * The size of this collection.
     * @return The number of elements added to this object.
     */
    public int size(){
        size = collection.size();
        
        if (size == 0){
            return 0;
        }
        else
        return size;
    }
    
    /**
     * Add aNumber to the collection while maintaining a natural ordering.
     * Precondition: The computer has enough memory to either add aNumber.
     *
     * @param aNumber is the datum to add to this object without rounding it.
     */
    public void addInOrder(double aNumber){
        
        collection.add(aNumber);
    }
    

User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: Collection Of Doubles
23 Sep, 2008 - 06:02 PM
Post #2

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
yup that works just fine
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Collection Of Doubles
23 Sep, 2008 - 06:04 PM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
The problem is because in your constructor you set the collection to null. This changes it from an arraylist to a null and thus no longer can you use size etc. Comment out that line in your constructor and you should see it starting to work just fine.

Hope it works out for you.

"At DIC we be arraylist sizing code ninjas... we deal in everything big, it is how we roll" decap.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:56AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month