Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

dhemmy

  • 2 years ago

how do i write a method that takes an integer and an integer array and returns a Boolean indicating whether the integer can be found in the array or not in java

  • This Question is Open
  1. bdean20
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    If you can use an ArrayList instead, you can use the existing method 'contains'. If not, you could copy the implementation in java: public boolean contains(Object o) { return indexOf(o) >= 0; } where indexOf is: public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; } If you know the list is sorted, you can use a binary search.

  2. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy