Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 862291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:07:05+00:00 2026-05-15T09:07:05+00:00

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

  • 0

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

EDIT:(part 2 of the question)

Thanks for the replies. They have been very helpful.
I’ve actually run into this situation more than once, and I’d like to give a second example that is unrelated to inheritance. This is actually a real-world example, and though I’ve decided my current project is small scale enough to be safe with using vectors based on your answers, it’s a concept that I imagine I’ll use on a much larger scale at some point.

In creating an engine for my current Android game project, I found it necessary to create an Imagepoint object that is basically a set of XY coordinates that are used to track significant parts of a sprite image. Say you have a bone character made up of several bitmaps, it’s useful to know where the neck attaches to the torso, the forearm to the bicep, etc. This xy offset data is used later to calculate the position for various purposes, such as where to position other sprites by using trigonometric functions to find the xy offset given the current angle.

Most sprite objects will need only one image point. The bicep needs one for the forearm, the forearm needs one for the hand, the neck needs one for the head, etc. The torsos in my current character models are the exception, and they need several for the shoulders, the legs, the neck, and any removable decorative sprites.

The sprite is the object that contains the imagepoint vectors, there will be no inherited classes, because each : torso, leg, etc is simply an instance of the sprite class, which is strictly a graphical object, containing a pointer to the bitmap, these imagepoints, and assorted position/orientation data, and will not be used for any specialized purposes. If I were to use this concept on a large scale game with massive numbers of instances, where most would require only a single imagepoint, with a few objects requiring several or none, and where there would be no special cases meriting the use of inheritance. What would your thoughts be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:07:06+00:00Added an answer on May 15, 2026 at 9:07 am

    Yes and no.

    I mean your question really depends on how are you going to use these objects.. personally I like to split behaviours and attributes as much as possible to “clusterize” them according to the kind of object they represent without caring about reusing some code a little bit less compared to a really inheritance approach like the one you proposed. Something like:

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

    And so on. This approach is like “having just exactly what you need for your objects without force anything to be a specific case”. Of course this means that it will have two different implementations of nod() but when you don’t need to optimize (and you should care about it only at the end) just stick with your personal preference, assuming that it doesn’t go against any standard, widely accepted convention.

    You don’t have to make anything inherit from a single object, just customize the hierarchy as you like.. also a composite solution (like already suggested) would be a solution also if the concept of “head part” is not the same for your Dragon and for your Hydra..

    A third solution for your specific case could be to use a LinkedList instead that an ArrayList. In this case whenever you have just one head you won’t waste space (except for instantiation of the list) and you’ll have just the next pointer of the head (of the list 😀 ) pointing nowhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Clicking on an element which has a Javascript handler makes the element go have
I have a library with an API much like this: public class Library :
Say I have these two models: public class City extends Model { @ManyToOne private
I have a list l:List[T1] and currently im doing the following: myfun : T1
just saw this comment in a what JS lib do you use poll @Xanti
Quick background I have a concurrent map I used to cache some values that
Agile emphasizes quick iterations without wasteful planning. MVC emphasizes separation of concerns based on
In the following examples: the first seems more verbose but less wasteful of resources
I would like to check to see if I have an Internet connection on
For some time I have used tabstop=4 in my source files. Now that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.