+ 1
Hi, how can i compare more than three vehicle with reference- types?
for two vehicle we use Vehicle getfastercarmodule(Vehicle x){ if(this.speed > x.speed) return this; return x; } int getfastercar(Vehicle x){ if(this.speed > x.speed) return this.speed; return x.speed; } Vehicle j; Vehicle o; j = new Vehicle("benz", 300); o = new Vehicle("bmw", 250); System.out.println("the faster car is"+" "+ j.getfastercarmodule (o).getModule()); System.out.println("the speed of car is"+ " "+j.getfastercar
2 ответов
+ 1
id use a nested HashList (or HashMap.. im not sure about the right name of it). instatiate a list for every car you want to be compared with the key-value pair of car and speed. then put these lists in another list (this one yould also be a "normal" ArrayList) and iterate through it with a for- or foreach loop.
0
Iterating over a List is sufficent for comparing vehicles.
If you implement the comparable interface in your Vehicle Class you can even sort the list with Collection.sort()