1

Say for example, I want to get the index of "w" in "howdy", or the "g", in "wasgood", how would i do that? The Arraylist .indexOf method only seems to return the entire element stored in the Arraylist

ArrayList <String> list = new ArrayList<String>();

list.add("hi");
list.add("howdy");
list.add("greetings");
list.add("wasgood");
list.add("squareup");
list.add("plurals");
5
  • 1
    Try to use chartAt() method Commented Mar 3, 2016 at 7:23
  • To get the index first you need to get the element from array list .. Commented Mar 3, 2016 at 7:23
  • 1
    could someone give me an example code? Commented Mar 3, 2016 at 7:25
  • @PrinceManiGupta Do we really need to convert to char array to get the index?? Commented Mar 3, 2016 at 7:25
  • @AnoopLL.my bad.No need to do that.indexOf will save this effort. Commented Mar 3, 2016 at 7:28

3 Answers 3

4

This gets you position in arraylist of greetings.

list.indexOf("greetings");

This get you position of letter in word.

"greetings".indexOf("t");
Sign up to request clarification or add additional context in comments.

Comments

0

"howdy".getIndexOf("w"); will return the index of 'w' in "howdy".

2 Comments

how could I make it so that I can cycle through all the stored Strings, instead of typing out each one individually?
@Picklejar75if you want to cycle through the list, then you would have to find index of a particular character for every item of the list. For example : You ll have to find index of 'w' in all the list items. If you want to find the index of different characters in different items, then you have to do it manually.
0

It looks like you want to know the char index in a string. What you should use is like "howdy".indexOf('w'). It has no relation to ArrayList. It is a method of String.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.