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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:33:26+00:00 2026-06-04T18:33:26+00:00

As a sample, I am developing a simple MySortedSet in java which implements SortedSet

  • 0

As a sample, I am developing a simple MySortedSet in java which implements SortedSet interface. It is backed up with a simple array which is E[] array.

I have several questions regarding that:

This is the class: (I am not writing entire code, instead of related parts)

public class MySortedSet<E> implements SortedSet<E>, Iterator<E> {

 private E[] array;
 private Comparator<? super E> _comparator;
 private int size = 0;
 private int capacity;

 @SuppressWarnings("unchecked")
 public MySortedSet() {
    this.capacity = 10;
    this.array = (E[]) new Object[this.capacity];
    // this.array = Array.newInstance(Class<E> var,int size);
    // We have to get Class<E> from outside caller.
 }
}

Since it accepts all sort of type from primitive to reference types etc. I am not really sure when removing an item, assigning null is a good way in place of the removed item. Since Java initializes primitive types with 0. So null only works for reference types.

Below is probably very bad design:

@Override
public boolean remove(Object o) {
    int indexOfElement = this.find(o);
    boolean removed = false;
    if (indexOfElement != -1) {
        this.array[indexOfElement] = null;
        removed = true;
    }
    return removed;
}

Can someone tell me what the best way is to remove an element from an array?

Edit:

Honestly what I am thinking to remove an element from an simple array is like copy the entire array without the removed item into a whole new array but I am not sure how efficient it would be in terms of performance and etc.

  • 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-04T18:33:27+00:00Added an answer on June 4, 2026 at 6:33 pm

    It kinda depends on the context of how you want to use your array. For example, if you are going to be iterating over the array and using the contents of it for standard methods like Arrays.sort(), they might generate NullPointerExceptions if you have null values in your array.

    If you really want to remove items from an array in a safe way, I’d suggest changing your array to an ArrayList like this…

    ArrayList<Object> list = new ArrayList<Object>();
    list.add(object);
    list.remove(object);
    

    As this will actually remove the item from the list completely – no nulls or anything will remain, and performing methods like length() will return a real value.

    For instances when I have used an array, I set the value to null, and ensure that all iterations over the array check that value != null before I try to query it. After setting the nulls for the removed items, I usually loop over the array and manually sort all the nulls to the end of the array, and then do System.arraycopy() to resize the array. This will leave you with a new array of the correct size, with all items in it except for the removed ones. However, I suggest this only if you really must use an array, as it is slower and introduces much greater potential for errors and NullPointerExceptions.

    Alternatively, if you’re not worried about sort-order, you can simple move the last item in the array over the top of the item you want to remove, and keep a count of the real array size. For example…

    Object[] array = new Object[20];
    int realSize = 15; // real number of items in the array
    
    public void remove(int arrayIndex){
        array[arrayIndex] = array[realSize-1];
        realSize--;
    }
    

    This method removes an item in the array by ‘replacing’ it with the item in the last position of the array – its very quick and pretty to implement, if you don’t care about sort order.

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

Sidebar

Related Questions

As a sample, I am developing a simple MySortedSet in java which implements SortedSet
I am developing a simple sip-based voip app for android. I used this sample
Can anyone recommend a good Java game engine for developing simple tile-based games? I'm
I developing the simple UIApplication in which i want to crop the UIImage (in
I am developing a simple api which will be used by iphone. Is there
I'm developing a simple page with Symfony2, using Twig as template engine. I have
I am developing sample application that uses Google Map API. I have generated Google
I'm developing simple MVC app in Cocoa/Objective-C. I have a strange issue (or misunderstanding)
I have a simple 250k flash movie a page i'm developing. The flash movie
im developing a simple Mxml application using flex 3. I have used simple text

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.