3

I am starting to learn Java and would like to know how to loop through instances of a class when calling a method, instead of separately calling the method for each instance, like below:

String potentialFlight = (Flight1.getLocations(response));
if (potentialFlight != null) {
    System.out.println(potentialFlight);
}

potentialFlight = (Flight2.getLocations(response));
if (potentialFlight != null) {
    System.out.println(potentialFlight);
}

For clarity, Flight1 and Flight2 are instances of a class Flight. Response is the user input parsed into the method and will be a location, which I will use the getLocations method to return any potential flights departing from that location.

If you need more of my code, please comment below.

Thanks for you help!

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
NatalieHants
  • 57
  • 1
  • 7
  • When you start coding classes or methods like `Thing1`, `Thing2`... something is usually very wrong somewhere. Why do you even have such classes? – Tunaki Apr 28 '16 at 21:56
  • if `Flight1`and `Flight2` are instances of a class, they should be named `flight1` and `flight2`. Put them in a list or array, and loop over that list or array. – JB Nizet Apr 28 '16 at 22:00
  • 1
    @Tunaki he/she said they're instances of the same class, I'm hoping it's just him/her not sticking to variable naming conventions. Anyway, you should be storing them in some sort of collection, be it an array or something similar. Then you can loop through the instances and perform the method call. – James Buck Apr 28 '16 at 22:02
  • Yes, sorry new to coding and didn't realise that the correct naming convention was lowercase! Thanks for your help. – NatalieHants Apr 29 '16 at 08:14

3 Answers3

6

You could put all your instances into an Array(List), and use a foreach construction to iterate over all instances.

For example:

Flight[] flights = { Flight1, Flight2 };
for (Flight f : flights) {
    String potentialFlight = (f.getLocations(response));
    if (potentialFlight != null) {
        System.out.println(potentialFlight);
    }
}
Rick
  • 443
  • 2
  • 10
2

solution:

Stream.of(flights)
        .map(f -> f.getLocations(response))
        .filter(f -> f != null)
        .forEach(System.out::println);
Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
0

You should probably use interfaces for flexible handling e.g.:

public interface IFlyingObject {

    Object getLocations(Object o);

}

public class Plane implements IFlyingObject{
    @Override
    public Object getLocations(Object o) {
        return null;
    }
}
public class Helicopter implements IFlyingObject{
    @Override
    public Object getLocations(Object o) {
        return null;
    }
}
public static void main(String[] args) {
    List<IFlyingObject> flightObjects = new ArrayList<IFlyingObject>();
    flightObjects.add(new Plane());
    flightObjects.add(new Helicopter());
    Object response = new Object();
    for (IFlyingObject f : flightObjects) {
        Object result = f.getLocations(response);
    }
}
Mad Matts
  • 1,118
  • 10
  • 14