Hi, how can i compare more than three vehicle with reference- types? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

2nd Dec 2016, 5:01 PM
ehsan shahbazi
ehsan shahbazi - avatar
2 Answers
+ 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.
2nd Dec 2016, 5:33 PM
Thomas Artmann
Thomas Artmann - avatar
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()
2nd Dec 2016, 7:40 PM
Roland