I have defined an array of objects for a class Plane
. Like this:
Plane[] terminalOne = new Plane[] {
new Plane(1, "Madrid", "Ryanair", "Airbus A300", "05.00"),
new Plane(3, "Riga", "AirBaltic", "Boeing 737", "05.30")
//ETC..
};
I'm trying to figure out how to manipulate / get information from this array, for example, display objects. I tried System.out.println(terminalOne);
which returns [Lairport.Plane;@322ba3e4
(where airport is my package) I don't understand what this means, but I assume it returned first object? I tried to make it more readable and in my file where I define Plane class and object constructor I added this function:
public void displayPlane() // display plane
{
System.out.println();
System.out.print("{" + flightID + "," + destination + "," + airline + "," + aircraft + "," + time + "}");
System.out.println();
}
To display information about object in form of {.., .., .., .., ..}
and tried applying it in my main file as terminalOne.displayPlane();
However got a compiler error saying "Can not find symbol, symbol: method displayPlane(), location: variable terminalOne of type Plane[]"
I worked with LinkedLists where I defined these methods in a separate file, alongside methods for search, delete etc.. Can I do something like this for arrays, if so what is the correct way?
terminalOne
is an array, not an individual plane. You could use:
for (Plane plane : terminalOne) {
plane.displayPlane();
}
... but I would personally consider overriding toString()
in Plane
instead:
@Override public String toString() {
return "{" + flightID + "," + destination + "," + airline + "," +
aircraft + "," + time + "}";
}
Then:
for (Plane plane : terminalOne) {
System.out.println(plane);
}
See more on this question at Stackoverflow