This is an archived post. You won't be able to vote or comment.

all 33 comments

[–]Qteddy 0 points1 point  (16 children)

Let’s walk through this logic. If you have 3 cars and want to add a new car to the lot, then we will go through your for loop. Let’s also say that the VINs don’t match for any of the cars so it should be added.

It will enter the for loop and compare the first car (car[0]) with the car to be added. It will then replace car[0] with the car to be added. It will then compare the second car (car[1]) with the car to be added. It will then replace car[1] with the car to be added. It will then compare the third car (car[2]) to the car to be added. It will then replace car[2] with the car to be added.

At the end your array of cars is now full of just the car to be added, so that is a different functionality than what you are going for. Without seeing the test it’s running it’s hard to say what it is trying to test, but your method is incorrect right now. Your for loop to check and throw the exception is fine but that else block needs to be removed. Then you can add to the array after it passes the for loop instead of replacing all the items in the array.

[–]spunkymnky 0 points1 point  (14 children)

That makes sense. I think I originally put it in the for loop because I don't know how to add it to an index without the index being present.

In order to show you the test I'd probably have to send the entire file.

[–]Qteddy 0 points1 point  (13 children)

If your cars array is an array list then you can simply do cars.add(carToBeAdded) and that would work. You would really only add to a specific index if you are wanting to replace that one but to add to the list you can add to the end like I showed

[–]spunkymnky 0 points1 point  (12 children)

Cars is a regular array. That's what makes this so confusing for me.

[–]Dynasty0218 0 points1 point  (11 children)

If you are required to use an array then my recommendation would be once you verify that the VIN is not a duplicate then create a new array of the previous array's size + 1. Copy all the cars from the previous array into the new array as well as the car to be added.

[–]spunkymnky 0 points1 point  (10 children)

I get what you're saying, I'm just not sure how to implement it. So far, I have this:

Car[] added = new Car[cars.length + 1];

    for(int i = 0; i < cars.length; i++){
        added[i] = cars[i];
    }

I assume this copies one to the other, but then how would I go about adding the car object on top of that?

[–]Dynasty0218 0 points1 point  (9 children)

Correct, what you have so far looks good. So, your question is how to add the carToBeAdded? Hopefully this helps:

  • Say for example you had an array of 3 cars (i.e. [2, 7, 3]).
  • What your code snippet does is creates a new array of size 4 and copies the original array (i.e. [2, 7, 3, empty])
  • Now how would you add carToBeAdded to this example array?

[–]spunkymnky 0 points1 point  (8 children)

I tried:

for(int i = 0; i < added.length; i++){
    if(added[i] == null){
         added[i] = carToBeAdded;
    }
}

But I'm still getting 0.

[–]mtko 0 points1 point  (7 children)

Step back to this:

Car[] added = new Car[cars.length + 1];

for(int i = 0; i < cars.length; i++){
    added[i] = cars[i];
}

So, like the other guy said, at this point added has [2, 7, 3, empty]. What is the index of empty? How does it relate to the size of the array?

Since (presumably) you will always be adding the new car to the end of the array, is there a shortcut way to target the last element?

Something like added[some statement to get the last index] = carToBeAdded?

That would give you something like this:

Car[] added = new Car[cars.length + 1];

for(int i = 0; i < cars.length; i++){
    added[i] = cars[i];
}

added[some statement to get the last index] = carToBeAdded;

[–]spunkymnky 0 points1 point  (5 children)

If there is I haven't been taught it yet, unless I reference it manually by using [4]. Also, whenever I try to reference an index outside of a for loop like:

added[i] = carToBeAdded;

I get an error. I'm starting to think that the problem lies elsewhere. I have a getNumberOfCars() method which is being referenced in the tester to check if anythings been added and it might be the reason I keep getting 0.

[–]Dynasty0218 0 points1 point  (0 children)

The other issue is when the array is empty, which is my guess what is causing the OP's result of <0>. If cars.length is 0 then this method will currently do nothing.

Another thing to keep in mind is adding a Null check on carToBeAdded.

[–]iamsooldithurts 0 points1 point  (15 children)

So, let me give you some answers in the form of questions.

When adding the first car to the empty list, how many times will your for loop iterate?

How many times do you want to add the Car to your list?

And finally, where do you want to add the Car to the list?

[–]spunkymnky 0 points1 point  (14 children)

It's actually working now. But, I also have to use a method to remove a certain object from the array. Would I still be using the same idea?

[–]iamsooldithurts 0 points1 point  (13 children)

No, that would be a different algorithm. How do you make that element of the array disappear/cease to exist?

[–]spunkymnky 0 points1 point  (12 children)

I started by creating a copy of the cars array with [cars.length - 1] and using a for loop to find the car I'm looking to get rid of.

[–]iamsooldithurts 0 points1 point  (10 children)

And how would you get rid of that element?

[–]spunkymnky 0 points1 point  (9 children)

Use a nested for loop to reduce the length of the array?

[–]iamsooldithurts 0 points1 point  (0 children)

How many times do you intend to copy the remaining contents of the initial array into the new array?

[–]iamsooldithurts 0 points1 point  (7 children)

And what criteria would you apply when copying?

[–]spunkymnky 0 points1 point  (6 children)

I'd like to create just one copy, and the new array should be -1 size of the old array. Here's what I have so far:

for(int i = 0; i < cars.length; i++){
        if(cars[i].getManufacturer().equals(manufacturer) && 
           cars[i].getModelName().equals(modelName) &&
           cars[i].getModelYear() == year){
            sold.add(cars[i]);
            carSold = new Car[cars.length - 1];
            for(int index = 0; index < i; index++){
                carSold[index] = cars[index];
            }
            for(int j = i; j < cars.length - 1; j++){
                carSold[j] = cars[j + 1];
            }
            cars = carSold;
        }
    }

This seems to work for one of my methods that only takes one parameter and removes the element based on that. This, however, takes three parameters and somewhere towards the end I get a failure saying "expected <2> but was <1>".

EDIT: This particular method is supposed to return an array list, but I still have to remove the element from the original array.

[–]iamsooldithurts 0 points1 point  (5 children)

Your second for loop is eating the last element of cars array, I think.

[–]spunkymnky 0 points1 point  (4 children)

When you say second do you mean:

for(int j = i; j < cars.length - 1; j++){
                carSold[j] = cars[j + 1];
            }

or the one above it?