Java Question

How does indexOf method works in Java?
Gurpreet KaurAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim CakalicSenior Developer/ArchitectCommented:
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
krakatoaCommented:
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.
Gurpreet KaurAuthor Commented:
Then I think the method should be named as  positionInIndex.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

krakatoaCommented:
Then why not send your suggested improvement over to Oracle for possible adoption?

The point of the indexOf("x") method - at the moment -  is that it returns exactly what you asked it for - viz. the index of the particular token in the sequence in question.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim CakalicSenior Developer/ArchitectCommented:
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
krakatoaCommented:
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?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.