Im studying the book programming in BlueJ (Barnes/Kolling 5th ed.) and i am stuck at question 4.51.
I´ll try to explain:
There is an arraylist and I must get an object with a specific number, not an indexnumber, but a number which is one of the fields of the objects in the arraylist.
So the header of the method I am trying to write is:
but the number is an attribute of the objects (Lots) in the arraylist, not the indexnumber. I have no idea how to get the right Lot out of the arraylist. I only know how to access an object in an arraylist via its indexnumber. I hope you can help, I can provide more code from this project if neccesary.
sonia jiem wrote:I have no idea how to get the right Lot out of the arraylist. I only know how to access an object in an arraylist via its indexnumber.
You have to iterate over the arraylist, using directly a for loop of using an Iterator (looping on the next()). For each object in the list you have to get that field and compare it with the requested value. Once found, you have the object and you can return it.