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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:48:35+00:00 2026-06-19T02:48:35+00:00

I’m trying to compress an array of objects that will have empty items interspersed

  • 0

I’m trying to compress an array of objects that will have empty items interspersed with complete items. I want to put all the full elements at the start in the same order they started with, and the empty elements on the end.

The object in question uses a String field, “name”, and an int field, “weight”. An empty version has “no name” and 0 respectively. So an array of the type the method needs to deal with will contain something like:

  • Fred | 4
  • Bob | 3
  • no name | 0
  • Gina | 9
  • no name | 0
  • Yuki | 7

After feeding through the method, the array should go Fred, Bob, Gina, Yuki, no name, no name.

My thought for step one was to just figure out which were full and which weren’t, so I came up with this:

public void consolidate() {
    boolean[] fullSlots = new boolean[spaces.length];

    // pass 1: find empties
    for (int i = 0; i < spaces.length; i++) {
        fullSlots[i] = spaces[i].getName().equals("no name");
    }
}

spaces is the array of objects, getName() retrieves the name field from the object.

I’m not sure where to go from here. Suggestions?

EDIT: Okay, here’s what Infested came up with:

public void consolidate()
{
    int numberOfEmpties = 0, spacesLength = spaces.length;
    Chicken[] spaces2 = new Chicken[spacesLength];
    for(int i = 0; i < spaces.length; i++)
    {
        spaces2[i] = new Chicken(spaces[i].getName(),
                                        spaces[i].getWeight());
    }

    // pass 1: find empties
    for (int i = 0, j = 0; i < spacesLength; i++)
    {
        if (spaces2[i].getName().equals("no name") == false)
        {
            spaces[j] = new Chicken(spaces2[i].getName(),
                                        spaces2[i].getWeight());
            j++;
        }
        else
        {
            numberOfEmpties++;
        }
    }
    for (int i = spacesLength - 1; numberOfEmpties > 0 ; numberOfEmpties--, i--)
    {
       spaces[i] = new Chicken("no name", 0);
    }
}

Tested and working.

  • 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-06-19T02:48:37+00:00Added an answer on June 19, 2026 at 2:48 am

    ill assume its a method of the class:

    public void consolidate()
    {
        int lengthOfSpaces = spaces.length , i, numberOfEmpties = 0;
        Type[] spacesNumberTwo = new Type[lengthOfSpaces ];
    
        // pass 1: find empties
        for (i = 0; i < lengthOfSpaces ; i++)
        {
            if(spaces[i].getName().equals("no name") == false)
                 spacesNumberTwo[i] = new Type(spaces[i].getName(), spaces[i].getInt());
            else
              numberOfEmpties++;
        }
        for (i = lengthOfSpaces - 1; numberOfEmpties > 0 ; numberOfEmpties--, i--)
        {
               spacesNumberTwo[i] = new Type("no name", 0);
        }
        spaces = spacesNumberTwo
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

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.