Java Question

How does indexOf method works in Java?
Gurpreet KaurAsked:
Who is Participating?
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
1
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.
0
Gurpreet KaurAuthor Commented:
Then I think the method should be named as  positionInIndex.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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.
0

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
0
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?
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.