-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA-lab1(b)-arraylistwithTIMECOMPLEXITY
More file actions
60 lines (50 loc) · 1.82 KB
/
DSA-lab1(b)-arraylistwithTIMECOMPLEXITY
File metadata and controls
60 lines (50 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.ArrayList;
import java.util.Scanner;
public class VehicleinfoArrayList {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
ArrayList<Integer> wheels = new ArrayList<>();
ArrayList<Integer> enginecc = new ArrayList<>();
names.add("Bike");
wheels.add(2);
enginecc.add(150);
names.add("Car");
wheels.add(4);
enginecc.add(1500);
names.add("Truck");
wheels.add(6);
enginecc.add(5000);
names.add("Scooter");
wheels.add(2);
enginecc.add(125);
names.add("Bus");
wheels.add(8);
enginecc.add(7000);
Scanner scanner = new Scanner(System.in);
System.out.println("Available vehicles:");
for (String name : names) {
System.out.println("- " + name);
}
System.out.print("Enter the name of the vehicle to get details: ");
String inputName = scanner.nextLine().trim();
long startTime = System.nanoTime();
boolean found = false;
for (int i = 0; i < names.size(); i++) {
if (names.get(i).equalsIgnoreCase(inputName)) {
System.out.println("Vehicle Details:");
System.out.println("Name: " + names.get(i));
System.out.println("Number of wheels: " + wheels.get(i));
System.out.println("Engine CC: " + enginecc.get(i));
found = true;
break;
}
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
if (!found) {
System.out.println("Vehicle not found.");
}
System.out.println("Time taken to search and display: " + duration + " nanoseconds");
scanner.close();
}
}