2 Replies - 330 Views - Last Post: 23 September 2008 - 07:04 PM Rate Topic: -----

#1 anorman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 31-January 08

Collection of doubles

Post icon  Posted 23 September 2008 - 06:54 PM

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!

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


Is This A Good Question/Topic? 0
  • +

Replies To: Collection of doubles

#2 Mach1Guy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: Collection of doubles

Posted 23 September 2008 - 07:02 PM

yup that works just fine
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4190
  • View blog
  • Posts: 11,864
  • Joined: 18-April 07

Re: Collection of doubles

Posted 23 September 2008 - 07:04 PM

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" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1