Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Find the nearest free iterator

1. ## Find the nearest free iterator

So I am not sure if I am doing it correctly. I need to iterate through the list of an ambulance and then check if it is free. Then work out the distance of each area and return which is the nearest!

I need help with it.

```/**
* Find the nearest free ambulance to the given area.
* Distance is measured by the absolute value of the
* difference between two areas.
* For instance, the distance between areas 1 and 5 is 4,
* and the distance between areas 6 and 4 is 2.
* If more than one ambulance is free and nearest to the given
* area then the one with the lowest ID must be returned.
* @param area Look for the ambulance nearest to this area.
* @return the nearest ambulance, or null if there are no free ambulances.
*/
public Ambulance findNearestFree(int area)
{
Iterator<Ambulance> ab = cars.iterator();
while(ab.hasNext()){
Ambulance ambulacne = ab.next();
if(ambulacne.getArea() == area){

}
}
return null;
}```

2. ## Re: Find the nearest free iterator

1)check if it is free.
2)Then work out the distance of each area and
3)return which is the nearest!
How do you do each of those three items in the above list?

3. ## Re: Find the nearest free iterator

So how I can check if it is free?

4. ## Re: Find the nearest free iterator

What fields and methods does the class have? Do any of them have a value that says it is free?

5. ## Re: Find the nearest free iterator

so I need to run through the list and find which is free. Does not

```public class AmbulanceControl
{
private ArrayList<Ambulance> cars;

public AmbulanceControl()
{
cars = new ArrayList<Ambulance>();

}
public Ambulance findNearestFree(int area)
{
Iterator<Ambulance> ab = cars.iterator();
while(ab.hasNext()){
Ambulance ambulacne = ab.next();
if(ambulacne.getArea() == area){
return ambulacne;
}

}
return null;
}

}```

--- Update ---

Then I got test contention to test

```

@Test
/**
* Find the nearest ambulance when there are none free.
*/
public void findAmbulanceNoneFree()
{
control = new AmbulanceControl();
makeAmbulance(1, 1, false);
assertEquals(null, control.findNearestFree(1));
}

@Test
/**
* Find the nearest ambulance when there is one free.
*/
public void findAmbulanceOneFree()
{
control = new AmbulanceControl();
makeAmbulance(1, 1, false);
Ambulance ambulance = makeAmbulance(2, 1, true);
assertEquals(ambulance, control.findNearestFree(1));
}

@Test
/**
* Find the nearest ambulance when there is more than one free.
*/
public void findAmbulanceLowest()
{
control = new AmbulanceControl();
makeAmbulance(2, 1, true);
Ambulance ambulance = makeAmbulance(1, 1, true);
assertEquals(ambulance, control.findNearestFree(1));
}

@Test
/**
* Find the nearest ambulance when there are none.
*/
public void findAmbulanceLowestMultiple()
{
control = new AmbulanceControl();
for(int id = 5; id >= 2; id--) {
makeAmbulance(id, 1, true);
}
Ambulance ambulance = makeAmbulance(1, 1, true);
assertEquals(ambulance, control.findNearestFree(1));
}

@Test
/**
* Find the nearest ambulance in neighbouring areas.
*/
public void findAmbulanceNeighbours()
{
control = new AmbulanceControl();
Ambulance ambulance = makeAmbulance(1, 1, true);
makeAmbulance(2, 3, true);
assertEquals(ambulance, control.findNearestFree(2));
}

@Test
/**
* Find the nearest ambulance in neighbouring areas
* when only one is free.
*/
public void findAmbulanceNeighboursOneFree()
{
control = new AmbulanceControl();
makeAmbulance(1, 1, false);
Ambulance ambulance = makeAmbulance(2, 3, true);
assertEquals(ambulance, control.findNearestFree(2));
}

@Test
/**
* Find the nearest ambulance in neighbouring areas.
*/
public void findAmbulanceNeighboursFurther()
{
control = new AmbulanceControl();
makeAmbulance(1, 2, false);
Ambulance ambulance = makeAmbulance(5, 1, true);
assertEquals(ambulance, control.findNearestFree(3));
}```

6. ## Re: Find the nearest free iterator

Where is the code that tests if the Ambulance is free?
Why post all that code that has nothing to do with the problem of finding if the Ambulance is free?

Work on one problem at a time. How do you determine if an Ambulance is free?

Be sure to include these comments with your code:
```/**
* Find the nearest free ambulance to the given area.
* Distance is measured by the absolute value of the
* difference between two areas.
* For instance, the distance between areas 1 and 5 is 4,
* and the distance between areas 6 and 4 is 2.
* If more than one ambulance is free and nearest to the given
* area then the one with the lowest ID must be returned.
* @param area Look for the ambulance nearest to this area.
* @return the nearest ambulance, or null if there are no free ambulances.
*/
public Ambulance findNearestFree(int area)```

7. ## Re: Find the nearest free iterator

so i did in this way but i think it can be done better!!
[

8. ## Re: Find the nearest free iterator

Who wrote that code? Do you have a good helper now?

What happened to the iterator?

Note: math is a very poor name for a variable that holds the distance between two areas.
Also what is being held in the variable named: lengh?

NOTE: OP deleted the code from the last post