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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:49:49+00:00 2026-05-31T08:49:49+00:00

I have a user give me a random array of objects, I want to

  • 0

I have a user give me a random array of objects, I want to do some error checking and basically I want the null objects to be at the end of the array, so that the middle of the array is made up of only non-null objects (sorting of the objects doesn’t matter).

Here is what I have, it isn’t working.
Can anyone please help.

private void properArray(){
    int i = 0;
    int j;
    int cap = theHeap.length;
    for(; i < (cap-1); i++){
        if (theHeap[i] == null){
            j = i + 1;
            while(j < (cap-1)){
                if(theHeap[j] != null){
                    theHeap[i] = theHeap[j];
                    theHeap[j] = null;
                }
                j++;  
            }
        }
    } 
}
  • 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-31T08:49:51+00:00Added an answer on May 31, 2026 at 8:49 am

    Here’s a simpler way how you can sort such an array:

    Arrays.sort(theHeap, new Comparator() {
      public int compare(Object o1, Object o2) {
        // nulls are "greater" than non-nulls
        if (o1 == null && o2 != null) return 1;
        // non-nulls are "smaller" than nulls
        if (o1 != null && o2 == null) return -1;
        // in all other comparisons, we don't care
        return 0;
      }
    });
    

    Or with Java 8:

    Arrays.sort(theHeap, (o1, o2) -> (o1 == null && o2 != null) ?  1
                                   : (o1 != null && o2 == null) ? -1
                                   :                               0);
    

    If you have Apache Commons Collections on your classpath, you can write this with even less code:

    Arrays.sort(theHeap, new NullComparator());
    

    As Ted mentions, this performs in O(n log n) and creates a clone of your array for sorting… It is thus not the fastest solution…

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

Sidebar

Related Questions

I have a tree control and I want to give the user the ability
i have a wordpress blog and want to give people the same user experience
In a folder I have random module files. eg. user.rb that contains module User,
I'm programming a Monte-Carlo Simulation that should give the user quite some flexibility. Consequently,
i have user control, which i render on several views. i want to show
I'm writing a library that contains some hash functions. I want one of the
say if I wanted to give every user that registered on my site a
I have a users table, the user ID is public. But I want to
Basically I am trying to give a user a certain password so I can
I am using ASP.net(2.0) with VB.NET. I have a User registration form. On that

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.