I'm having trouble understanding the binarySearch method in the Arrays class when using an Object array. Is it possible to use this method to search for a specific field within an object in an object array?

For example, if my Object array is an array of Person objects, each with a String name, int age, and int unique identification number, then what would be the best way to return the name of a Person object given its unique identification number?

Yes, you can write your own Comparator to compare on the field you are wanting to search by and supply that as a parameter to the binarySearch() method.

You can find a straightforward example of that here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.