Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 926993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:46:45+00:00 2026-05-15T19:46:45+00:00

Is it possible to search an array of objects in Java by a private

  • 0

Is it possible to search an array of objects in Java by a private attribute with the Array.binarySearch method? I was thinking there must be something similar to the sorting technique, where you create a class that implements Comparator and pass this in to Array.sort, but I can’t seem to find anything (maybe there is something where instead of the compareTo method you just return the attribute used in the search)??

To be clear I have an array of anonymous Station objects, and this is passed to another class where I want to search the array for the name of the stations, which can be returned via a getName().

Any help would be much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:46:45+00:00Added an answer on May 15, 2026 at 7:46 pm

    Yes – in fact it uses Comparator, which you specified in your answer!

    If you have a look over the implementations of the binarySearch method in the API, you come across binarySearch(T[] a, T key, Comparator c) which:

    Searches the specified array for the specified object using the binary search algorithm.

    You can implement the Comparator however you like, and so can use it to compare the private attributes alluded to in your question.

    Edit responding to comment: The T is a generic parameter, meaning it can be anything, so long as it’s the same in every position it appears. In this case, it means that the first parameter must be an array of the second parameter’s type. Or in other words, if you’re sorting an array of Ts (Stations in your case) then you need to pass in an instance of that class (Station here) to act as the object to compare against. This key argument will always be passed in as one of the arguments to the comparator’s compare method.

    So I suspect in your case you were passing in a String representing the station name; you should instead pass in an instance of Station which has the appropriate name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 508k
  • Answers 508k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In manifest: <receiver android:name=".TimeZoneChangedReceiver" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.TIMEZONE_CHANGED" /> </intent-filter>… May 16, 2026 at 4:22 pm
  • Editorial Team
    Editorial Team added an answer Your call to swap() should include ampersands: swap(&a,&b); swap is… May 16, 2026 at 4:22 pm
  • Editorial Team
    Editorial Team added an answer Ryan Bates from railscasts.com shows in Episode #227 - Upgrading… May 16, 2026 at 4:22 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Would it be possible to use Array.indexOf() to search through an Array by the
I have an array of 1000-2000 elements which are pointers to objects. I want
Is it possible to search users of Facebook by gender, hometown and a group
I need to search Facebook for certain keywords and count them if possible? Thanks!
Before I look into doing this myself, is there already a function out there
I have a problem that i want to search in the specific locations in
Is it possible to call the built-in Video Recorder in Android through an implicit
From a quick Google search and a the wikipedia article on Multiple Inheritance, which
I am asking this here in the hope that someone has maybe come across
I have a functionality that I wish to provide to a customer for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.