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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:26:32+00:00 2026-06-13T03:26:32+00:00

I know my add method is correct: public void add(Object object) { if (!contains(object)

  • 0

I know my add method is correct:

public void add(Object object) {
    if (!contains(object) && size !=maxObjects) {
        set[size] = object; size++;
    }
    else 
        System.out.println("Already exists.");
}

because I get print outs like:

Set [maxObjects=8, set=[a, 7, null, null, null, null, null, null], count=2]  
true  (I ask if it contains a value)  
false  "                            "  
Set [maxObjects=8, set=[a, 7, b, Valencia, 24, s, Victoria, null], count=7]  
Set [maxObjects=8, set=[a, 7, b, Valencia, 24, s, Victoria, 4234], count=8]  

I have tried two different remove methods that are both the same (one I created; the other I found on Stack in the most similar problem a few days ago.)

1st remove:

public boolean remove(Object object) {
    if (contains(object)) {
        object = null; 
        return true;
    }
    System.out.println("The object doesn't exist to delete.");
    return false;
}

The other remove:

public boolean remove(object object) {
    for (int i=0; i<(size-1); i++) {
        while (!contains(object) && size <= maxObjects) {
            set[i] = set[i+1]; size--; return true;
        }
    }
    System.out.println("Doesn't exist.");
    return false;
}

Any help would be amazing!

  • 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-13T03:26:33+00:00Added an answer on June 13, 2026 at 3:26 am

    You have to find the object in the array and then for example move the last object to that index (if it isn’t already the last) and decrement size.

    if (obj == null) return;
    
    for (int i = 0; i < size; i++) {
        if (obj.equals(set[i])) {
            set[i] = set[--size];
            break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to know if there is an automated method to add e-mail settings
Is this method correct? public List<Question> getQuestionSet(int i){ List<Question> questionSet = new ArrayList<Question>(); Cursor
I know instrumentation is a technique to add trace code dynamically into the methods
I know add() adds the specified (signed) amount of time to the given time
Why does C++ need and use pointers? I know they add power to the
Possible Duplicate: Close mysql connection (PHP) Please help me, I not know where add
I know one can add event listener for window.error. However when working with Iframes,
I know I can add service behaviors with some XML configuration, but I'd like
I know how to add SL page as a whole to existing ASP.net application.
I know how to add the Google Map on my restaurant's webpage, but how

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.