简体   繁体   English

如何使用ArrayList访问元素的特定索引?

[英]How do i access a specific index of an element using ArrayList?

Say for example, I want to get the index of "w" in "howdy", or the "g", in "wasgood", how would i do that? 例如,我想在“ howdy”中获得“ w”或在“ wasgood”中获得“ g”的索引,我该怎么做? The Arraylist .indexOf method only seems to return the entire element stored in the Arraylist Arraylist .indexOf方法似乎只返回存储在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");

This gets you position in arraylist of greetings. 这使您在问候的arraylist中的位置。

list.indexOf("greetings");

This get you position of letter in word. 这使您在单词中的字母位置。

"greetings".indexOf("t");

"howdy".getIndexOf("w"); will return the index of 'w' in "howdy". 将返回“ howdy”中“ w”的索引。

It looks like you want to know the char index in a string. 您似乎想知道字符串中的char索引。 What you should use is like "howdy".indexOf('w'). 您应该使用的就像“ howdy” .indexOf('w')。 It has no relation to ArrayList. 它与ArrayList没有关系。 It is a method of String. 它是String的一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM