I was wondering if I could get some advice please. I have the following code:
private static int getIndex(String regneeded) {
System.out.println("The reg passed to getIndex method is:" + regneeded);
if (arraylist.contains(regneeded)) {
int pos = arraylist.indexOf(regneeded);
System.out.println("The position of the vehicle in the array list is:" + pos);
}else {return -1;}
return 0;
}
What I am trying to do is search an Array of DeliveryVehicles, for a specific reg number (that I pass to the method) and then say what position in the ArrayList the object that contains this reg is located.
I know I have an issue as I am searching for a string within an ArrayList and what I have in my ArrayList are objects. So, just looking for any advice while I continue to research.
Thank you in advance. Apologies if this is basic I am very new to Java.
EDIT: To include further information on the objects contained in the ArrayList.
DeliveryVehicle objects are contained in the ArrayList. Each contain registration numbers (they have other attributes but I'm focusing on the registration number to understand this issue first). There are setters (setRegNum()) and getters (getRegNum()) in the base class DeliveryVehicle for the registration number, and I pass the registration number to the constructor when I create my vehicle object as follows:
DeliveryBike myVehicle = new DeliveryBike("21D789");
arraylist.add(myVehicle);
I have extended the base class to include a DeliveryBike class (as seen). The code I originally posted was a method in my controller class. So, I suppose I'm confused how to access the registration number within the DeliveryBike object.
Thank you.