This content has been marked as final. Show 2 replies
Depends on what you are learning and what the teacher is teaching (if a teacher.)
The most basic way is to create a loop that gets each item, extracts the instance of Empl, compares the data in that to what you are looking for. In terms of an 'index' you would use a for rather than foreach to sequence through the collection.
I suspect, without having looked at all, that there is probably some truely clever and convoluted way to do with via the api as well which probably involves creating at least one other class.
user11191663 wrote:Another possibility is to set up an ArrayList for each thing you want to search on and then every time you add an employee, add the piece of data you want as well, viz:
Since the user will never seen the contents of the arraylist, is there a way that I can search by string last/first name and will get the index location in int if the result is met?
As long as you always add them in the same order, the indexes will match, so you could do something like
Empl e = new Empl(1,"Jim", "Balu",88); list.add(e); firstNames.add(e.firstName());
to return the index of the name, if it's there (if not, it will return -1).
ians = firstNames.indexOf(nameToFind);
I wouldn't recommend this as standard practise, but it should be OK for the level you're at.
NOTE: indexOf() returns the index of the FIRST matching item. You may want to think about what you want to do if there are more than 1.