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

  • Home
  • SEARCH
  • 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 4107168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:28:14+00:00 2026-05-20T21:28:14+00:00

I have about 10+ classes, and each one has a LUMP_INDEX and SIZE static

  • 0

I have about 10+ classes, and each one has a LUMP_INDEX and SIZE static constant.
I want an array of each of these classes, where the size of the array is calculated using those two constants.
At the moment i have a function for each class to create the array, something along the lines of:

private Plane[] readPlanes()
{
    int count = header.lumps[Plane.LUMP_INDEX].filelen / Plane.SIZE;
    Plane[] planes = new Plane[count];
    for(int i = 0; i < count; i++)
        planes[i] = new Plane();

    return planes;
}

private Node[] readNodes()
{
    int count = header.lumps[Node.LUMP_INDEX].filelen / Node.SIZE;
    Node[] nodes = new Node[count];
    for(int i = 0; i < count; i++)
        nodes[i] = new Node();

    return nodes;
}

private Leaf[] readLeaves()
{
    int count = header.lumps[Leaf.LUMP_INDEX].filelen / Leaf.SIZE;
    Leaf[] leaves = new Leaf[count];
    for(int i = 0; i < count; i++)
        leaves[i] = new Leaf();

    return leaves;
}

etc.
There are 10 of these functions, and the only differences is the class type, so as you can see, there’s a ton of duplication.

Does any one have any ideas on how to avoid this duplication?
Thanks.
(I asked a similar question before, but i guess the way i asked it was a bit off)

  • 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-20T21:28:14+00:00Added an answer on May 20, 2026 at 9:28 pm

    Okey doke … I’ve tested this to make sure, and I believe it does what you’re looking for.

    You need an interface:

    public interface MyInterface
    {
        public int getSize();
        public int getLumpIndex();
    }
    

    Your classes implement that interface:

    public class Plane implements MyInterface
    {
    
        ...
        public int getSize()
        {
            return SIZE;
        }
    
        public int getLumpIndex()
        {
            return LUMP_INDEX;
        }
    
    }
    

    In the class that header is an instance of, you have …

    public <E extends MyInterface> E[] 
        getArray(Class<E> c, MyInterface foo)
    {
        int count = lumps[foo.getLumpIndex()].filelen / foo.getSize();
        E[] myArray = (E[]) Array.newInstance(c, count);
        for(int i = 0; i < count; i++)
             myArray[i] = c.newInstance();
        return myArray;
    }
    

    You could call it from say, your Plane class as:

    Plane[] p = header.getArray(Plane.class, this);
    

    I think? 🙂 Can someone look at this and see if I’m off?

    (EDIT: Becasue I’ve tested it now – That works)

    On an additional note, you could eliminate the getters in each class by making getArray() take the size and index as arguments:

    public <E extends MyInterface> E[] 
        getArray(Class<E> c, int size, int index)
    {
        int count = lumps[index].filelen / size;
        E[] myArray = (E[]) Array.newInstance(c, count);
        for(int i = 0; i < count; i++)
             myArray[i] = c.newInstance();
        return myArray;
    }
    

    And call it as:

    Plane p[] = header.getArray(Plane.class, SIZE, LUMP_INDEX);
    

    from inside your classes. The interface just becomes empty to provide the generic type and you don’t have to define the getter methods.

    OR (last edit I promise, but this does give you choices and explains a bit about generics)

    Ditch the interface. What this removes is some sanity checking because the method doesn’t care what type of object you give it:

    public <E> E[] 
        getArray(Class<E> c, int size, int index)
    {
        ...
    

    Now you don’t have to define the interface or implement it, you just call:

    Plane p[] = header.getArray(Plane.class, SIZE, LUMP_INDEX);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have about 50 images of 50 x 50 pixel size each. I want
I have a array with about 300 indexes and each index has about 8
In my database, Concessions have a many-to-one relationship with Firms (each Concession has a
What I'm talking about here are nested classes. Essentially, I have two classes that
I am really confused about object relationships! I have two classes Person and Address.
I have about 150 000 rows of data written to a database everyday. These
I have about 20 check boxes. When the user selects these and then uses
I have been trying to learn about classes in PHP and part of my
I have been working at a few different companies now and every one has
I have several dynamic web projects in my workspace, each contains classes and refers

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.