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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:18:03+00:00 2026-06-15T02:18:03+00:00

Below is part of the solution to an homework assignment I had, and I

  • 0

Below is part of the solution to an homework assignment I had, and I don’t know why he has two for loops. If it was me, I would have set if (carsParked[i] == c) then set carsParked[i]=null
I don’t understand why second for loop statement can anyone explain?
By the way, CarsParked array is a type of Car class which stores car objects that are parked.

public void driveOut(Car c)
{
    for (int i=0; i<carsParked.length; i++) // Loop through the carParked array
    {
        if(carsParked[i] == c) // Find Car c at index i
        {
            //carsParked, remove(c);
            for (int j=i; j<carsIn-1; j++)
            {
                carsParked[j] = carsParked[j+1];
            }
            carsParked[carsIn-1] = null; 
            carsIn = carsIn - 1;
        }
    }
}

This is how cars are parked

public void driveIn(Car c)
{
    if(carsIn < carsParked.length)
    {
        carsParked[carsIn] = c;
        carsIn = carsIn + 1;
    }
    else // error message
    {
         System.out.println("Park " + location + " is full, for " + c);
    }
}
  • 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-15T02:18:04+00:00Added an answer on June 15, 2026 at 2:18 am

    Say you have an array with 5 cars: [Car1, Car2, Car3, Car4, Car5], and you call the method driveOut(Car3).

    By setting the value to null when you find it, you end up with [Car1, Car2, null, Car4, Car5]. This practice can lead to NullPointerException type errors, because null values are in the middle of the array, and the data is not compacted.

    The solution is basically eliminating the gap by starting from that point and shifting the remaining cars to the left:

    Inner loop rev 1: [Car1, Car2, Car4, Car4, Car5]
    Inner loop rev 2: [Car1, Car2, Car4, Car5, Car5]
    

    Then outside the loop, the last entry is set to null, leaving you with [Car1, Car2, Car4, Car5, null]

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

Sidebar

Related Questions

I have part of the code below: while($array = $result->fetch_assoc() ){ $second_query = INSERT
I have a table part with a few demo data as below in Oracle
the picture below has 2 divs, 1 is the part where the login form
Note, I have found the solution to my problem. Please see below. I'm converting
I'm trying to implement the pseudo solution below in java as part of an
This is a two part question. The first part is converting the function below
I have a model, Director with two DateFields, and two subclasses (code below). I
First, I would like to say that this is for a homework assignment and
This is a two part question. I have a class that gets all processes
I have part of an application initialization string below: <Controls> <HtmlElement name=lnkLogIn type=HtmlElement> <AttributeMatchPath

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.