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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:09:43+00:00 2026-05-23T07:09:43+00:00

I’m stuck on a homework question… so far, using Arrays ONLY I’m having trouble

  • 0

I’m stuck on a homework question… so far, using Arrays ONLY I’m having trouble deleting an object in an array by setting it to a null object..I had 3 methods, one method to add an object, one method to return a specific object from an array, one method to delete..so far the add and return objects methods work..but not the delete method…some help guys??

this is the class for the arrays and methods…I’m testing the methods in a main method

public class Book {

    public void addContact(Contact[] contactBook)
    {
        int slots = 0;
        for(Contact i : contactBook)
            if (i == null)
                slots++;

        if(slots == 0)
            System.out.println("Contact book full..can't add anymore!");

        else
        {
            String name = Keyboard.readString("Enter name: ");
            int id = Keyboard.readInt("Enter "+name+"'s id: ");
            String classroom = Keyboard.readString("Enter "+name+"'s class: ");
            int number = Keyboard.readInt("Enter "+name+"'s mobile: ");

            for (int i = 0; i < contactBook.length; i++)
            {
                if(contactBook[i] == null){
                    contactBook[i] = new Contact(name,id,number,classroom);
                    break;
                }
            }
        }//end else

    }//end method

    public Contact getContact(Contact[] contactList)
    {
        Contact contact = null;
        int id = Keyboard.readInt("Enter student id: ");

        try
        {
            for(Contact i : contactList)
            {
                if(i.id == id)
                {
                    contact =i;
                    break;
                }
            }
        }
        catch(NullPointerException e)
        {
            System.out.println("Student ID:"+id+" does not exist..");
        }

        return contact;
    }//end getContact

    public void deleteContact(Contact[] contactList)
    {
        Contact delete = getContact(contactList);

        for(Contact i : contactList)
        {
            if(i!=null)
                if(delete.id == i.id)
                {
                    i = null;
                    break;
                }
        }
    }//end delete

    public static void main(String args[])
    {
        Contact[] contacts = new Contact[200];
        Book newBook = new Book();

        newBook.addContact(contacts);

        for (Contact i : contacts)
            if(i != null)
                System.out.println(i);

        newBook.deleteContact(contacts);

        for (Contact i : contacts)
            if(i != null)
                System.out.println(i);
    }
}

this is the class of the object
public class Contact {

    String name;
    String classroom;
    int id;
    int number;

    Contact(String name, int id, int number,String classroom)
    {
        this.name = name;
        this.id = id;
        this.number = number;
        this.classroom = classroom;
    }

    public String toString()
    {
        return ""+name+", student id: "+id+" class:"+classroom+" mobile:"+number;
    }

}
  • 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-23T07:09:44+00:00Added an answer on May 23, 2026 at 7:09 am

    The iterator does return a copy of the reference to the Contact object. You are setting this copy to null and not the object that is stored in the array.

    Use something like this:

    for(int i=0; i<contactList.size; i++) {
        if(delete.id == contactList[i].id) {
            contactList[i] = null;
            break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i got an object with contents of html markup in it, for example: string
I'm parsing an XML file, the creators of it stuck in a bunch social

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.