Is this a wasteful / poor design for using a vector / list, where in most cases will it have only one element?

Is this a wasteful / poor design for using a vector / list, where in most cases will it have only 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);
        }
    }
}

EDIT: (part 2 of the question)

Thanks for answers. They were very helpful. I actually came across this situation more than once, and I would like to give a second example that is not related to inheritance. This is really a real example, and although I decided that my current project is small enough to be safe using vectors based on your answers, this is a concept that I assume I will use on a much larger scale at some point.

Android, Imagepoint, XY-, -. , , , , , .. xy , , xy .

. , - , - .. , , , , .

- , , , : , .. , , , / - . , , , , . ?

+5
6

.

, , . , "" , , , , . - :

interface BigLizard
{
  void nod();
}

class Dragon implements BigLizard
{
  Head head;

  void nod() { head.upAndDown(); }
}

class ReallyScaryDragon extends Dragon { ... }

class Hydra implements BigLizard
{
  ArrayList<Head> heads;

  void nod() { for (Head h : heads) h.upAndDown(); }
}

. " , - , ". , , nod(), ( ), , , , .

- , . ( ) , " " Dragon Hydra..

LinkedList ArrayList. , , ( ), next (: D), .

+2

, //, .

, . , , , , , , .

say("Dragon, would you like a yummy princess?");
if(dragon.Stomach.Contents.IsEmpty)
{
    dragon.Nod();
}

:

say("Dragon, would you like a yummy princess?");
dragon.offerFood(princess);

// in dragon class
void offerFood(Food yummyFood)
{
   if(Stomach.Contents.Empty)
   {
        head.Nod(); // can be overridden by subclasses.
   }
}

-... , , ( , ), - , ( . , , !)

"", ( , yummy princess), ( ).

+2

, . , . , , , , . , , , , - , , , , . , , . , .

+1

nod() . , 1 5, , ( , ).

, ( ) - . ( , ). , , ; , / , .

+1

Head Composite type. , Dragon Head, Head GoUpAndDown().

0

. , ( - nod()). , , . , - , .

0

All Articles