is it wasteful/bad design to use a vector/list where in most instances it will only have one element

Posted by lucid on Stack Overflow See other posts from Stack Overflow or by lucid
Published on 2010-06-13T23:26:33Z Indexed on 2010/06/13 23:32 UTC
Read the original article Hit count: 441

Filed under:
|
|
|
|

is it wasteful/bad design to use a vector/list where in most instances it will only have one element?

example:

class dragon
{
    ArrayList<head> = new ArrayList<head> Heads;
    tail Tail = new tail();
    body Body = new body();

    dragon()
    {
        theHead=new head();
        Heads.add(theHead);
    }

    void nod()
    {
        for (int i=0;i<Heads.size();i++)
        {
            heads.get(i).GoUpAndDown();
        }
    }
}

class firedragon extends dragon
{
}

class icedragon extends dragon
{
}

class lightningdragon extends dragon
{
}

// 10 other one-headed dragon declarations here


class hydra extends dragon
{
    hydra()
    {
        anotherHead=new head();
        for (int i=0;i<2;i++)
        {
            Heads.add(anotherHead);
        }
    }
}

class superhydra extends dragon
{
    superhydra()
    {
        anotherHead=new head();
        for (int i=0;i<4;i++)
        {
            Heads.add(anotherHead);
        }
    }
}

© Stack Overflow or respective owner

Related posts about arrays

Related posts about memory