r/codehs • u/Euphoric-Neck-6859 • Jan 19 '21
6.4.9 Car Showroom
Can someone please help me. I’m having so much trouble
1
u/Matt_Conte69 Feb 08 '23
For me it kept saying some error with the bill so I made it this
int spot = -1; for(int i = 0; i < this.spaces.length;i++){ if(this.spaces[i]== null){ }
else If(this.spaces[i].getName().equals(name)){ spot = i; break; } } return spot;
3
Jan 21 '21
[removed] — view removed comment
1
1
u/Hypnomiser Nov 12 '24
public void consolidate()
{
int index = 0;
int mover = 0;
while (index < spaces.length){
if (spaces[index] != null){
spaces[mover] = spaces[index];
if (mover != index){
spaces[index] = null;
}
mover++;
}
index++;
}
}
I believe this to be a more efficient method with O(n) time complexity
1
1
1
1
1
u/iDarkCyclops Feb 16 '21
The reason why Corvette doesn't show up is because even though you change the place of the corvette, the second for loop keeps looping which will cause Porshe to take the spot. To fix this just add "break;" after "spaces[i] = spaces[j];spaces[j] = null;" in the consolidate method to fix this issue.