Link to home
Start Free TrialLog in
Avatar of Gurpreet Kaur
Gurpreet Kaur

asked on

Java Question

How does indexOf method works in Java?
Avatar of Jim Cakalic
Jim Cakalic
Flag of United States of America image

Hi.

Do you mean the String.indexOf method? I'll lead with this ... Here's a an article on understanding how to use the String indexOf method
https://blog.udemy.com/java-string-indexof/

Or maybe you were asking how it is implemented? Here's a link to the openJDK implementation
http://www.docjar.com/html/api/java/lang/String.java.html

If this isn't what you meant or wanted please point us in the right direction.

Regards,
Jim
indexOf() applies to :

AbstractList, ArrayList, CopyOnWriteArrayList, LinkedList, List, String, StringBuffer, StringBuilder and Vector.

Which one(s) are you interested in knowing the workings of?

. . .  but whichever one it is, the principle is the same : the above classes are all containers of discrete tokens the simplest of which is the string of "chars" which make up a String. indexOf() when applied to a String returns the position of the char that you provide to the funtion - so in the String "aabbxcc", indexOf("x") or indexOf('x') will return 4, as that is the position x in the String.
Avatar of Gurpreet Kaur
Gurpreet Kaur

ASKER

Then I think the method should be named as  positionInIndex.
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The name "positionInIndex" would, at least to me, seem to convey they idea that "index" is itself a collection of something along the lines of the alphabetical listing of topics/keywords commonly found in the appendix of a book. But in this context, "index" is already itself a position uniquely identifying one member "slot" within the referenced collection.

The dictionary I looked at had two references related to Computers
(a) a value that identifies and is used to locate a particular element within a data array or table.
(b) a reference table that contains the keys or references needed to address data items.

With respect to the "indexOf" name, the author's intent was most certainly definition (a).

Jim
I'm just curious : you awarded the points, but graded the answer "B", rather than "A".

What did you think was deficient about the answer can I ask?