views:

123

answers:

5

hi, i'm new to java programming and need to create a method called sizeofCarPark that returns the amount of cars that are stored in a Car Park, i'd appreciate some pointers in what i need to do. thanks. pieter

A: 

Just do yourList.size()?

Jack
+2  A: 

What kind of an Object is a Car Park?

ArrayList already has a size() method.

Example:

public class CarPark {
    private List<Car> cars = new ArrayList<Car>();

    public int sizeOfCarPark() {
        return cars.size();
    }
}
Rob Hruska
And if you're concerned about design, `cars` should probably be a `Set` instead of a `List` unless you specifically need to maintain ordering. But `Set` has a `size()` method, too.
Rob Hruska
That did it, much appreciated Rob. Pieter
Pieter
@Pieter - No problem. Future advice: your question is a bit vague, so try adding a little bit of detail in questions you might ask down the road. Adding a code snippet goes a long way.
Rob Hruska
A: 

http://download.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html The javadoc is always the best place to start. There's already a size() method that does it, so all your method needs to do is call on that and return its value.

AaronM
Might not hurt to link to a version of the API newer than 1.4.2. Java 1.4's End Of Life was in 2008.
Rob Hruska
Huh, you're right, that's really weird I didn't think Oracle would have kept that old documentation. To be honest I just used the one Google brought up for Arraylist, never crossed my mind that 6 wouldn't be at the top.The newest proper release one is - http://download.oracle.com/javase/6/docs/api/index.htmlAs far as I'm aware ArrayList hasn't changed so it should be the same, but better to use that API than the other one.
AaronM
@AaronM - Yeah, it probably hasn't changed. However, it's possible that a user drilling down through the docs might come across something that's deprecated or gone in a newer release. I always Google something like "java arraylist 6" to get the Java 6 version to be the first result. Also, the docs are probably still around because Oracle offers business support beyond the regular EOL. http://www.oracle.com/technetwork/java/eol-135779.html
Rob Hruska
+2  A: 

If your "CarPark" is-a collection of cars only:

int totalCars = myCarPark.size();

If your "CarPark" has-a collection of cars only (preferred solution!):

int totalCars = myCarPark.getCars().size();

If your "CarPark" is-a mixed collection, then you have to iterate and count:

int count = 0;
for (Object obj:myCarPark)
  if (obj instanceof Car) count++;

Hope it helps!

Andreas_D
A: 

I suppose you are meant to create a custom container class instead of using one of the Collection classes for that purpose.

Here's a little hint. Suppose you have a NumberBox class which stores a list of, err, ints. In order to maintain the number of elements, besides the array which holds the elements, you need some sort of counter. Let's call it size. Initially, size is set to 0. Then you have a method to add and remove elements, as well as a method which returns the current size. An implementation would look something like this (not a complete program, missing stuff like checking parameters e.g.).

public class NumberBox {
    private int[] list;
    private size = 0;

    public class NumberBox(int capacity) {
        list = new int[capacity];
    }

    public void add(int i) {
        // put the int at the first unoccupied position
        // increase the counter
    }

    public int remove() {
        // remove the last int which was inserted
        // decrement the counter
        // return it
    }

    public size() {
        return size; // return the current size
    }
}

Now what you need to do is think about how to apply this to your class.

Helper Method
Just curious, why would you do all of this yourself when the API already provides a `List` interface that does this and much more? If you need a custom wrapper, it could at least be backed by a `List`.
Rob Hruska