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 7582389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:25:26+00:00 2026-05-30T18:25:26+00:00

Say I want to add a value to an array, but it doesn’t matter

  • 0

Say I want to add a value to an array, but it doesn’t matter where, as long as the current item at that index is null. This null could be anywhere in the array, at random locations. I just want to have my value
somewhere in my array. Should I iterate over it like this:

    for (int i = 0; i < 64; i++) {
        if (items[i] == null) {
            items[i] = obj;
            return;
        }

This way it may have to iterate over a lot of elements before it finds a null spot. Would it be faster to use another type, i.e. ArrayList ?

This is for a game, so performance is very crusial, and this is done maybe 300 times per second (also accessing, and deleting items). So for a game, what should I use best to store items, to iterate over ?

  • 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-30T18:25:28+00:00Added an answer on May 30, 2026 at 6:25 pm

    One way I have used in the past is to use an array and a Queue. Any time you add a object to the array you first check your queue to see if you have any empty slots. Anytime you destroy a object you set the array slot to null and add the index to the queue so you can quickly pull it out later. During my tests this was faster than a List because you only resize the array if you make it bigger.

    code wise

    Queue<int> emptySlots;
    Object[] arrayOfStuff;
    
    public void AddStuff(object stuffToAdd)
    {
        if (this.emptySlots.Count > 0)
        {
            int index = this.emptySlots.Dequeue();
            //make sure your objects know their position in the array
            stuffToAdd.ID = index;
            this.arrayOfStuff[index] = stuffToAdd;            
        }
        else
        {
            //resize array and add new object to end rememebering to fetch its id
        }
    }
    
    public void RemoveStuff(object stuffToRemove)
    {
        this.emptySlots.Enqueue(stuffToRemove.ID);
        this.arrayOfStuff[stuffToRemove.ID] = null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add items in a LaTeX-document. Say for example, that I want
Say that I have an integer-indexed array of length 400, and I want to
Say I want to have a method that takes any kind of number, is
Let's say I want to create a collection class that is thread-safe by default.
Let's say I want to find the value of a cell in an Excel
Say you want a simple maze on an N by M grid, with one
Say you want to generate a matched list of identifiers and strings enum {
Say I want to copy the contents of a directory excluding files and folders
Say I want to create vb.net application in Visual studio 2005. What is the
Say I want to get the HTML of http://www.google.com as a String using some

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.