Java 如何检查正在循环的字符串的最后一个索引

Java 如何检查正在循环的字符串的最后一个索引,java,string,loops,Java,String,Loops,我知道循环是如何工作的,但我不确定如何检查在本例中迭代的字符串的最后一个索引。以下是一个例子: String words = "Apple gum fruit red orange triangle"; for(String foo : words.split("\\s+"){ //How would I get the index of foo at any given point in the loop } 你可以用柜台吗 String words = "Apple gum fr

我知道循环是如何工作的,但我不确定如何检查在本例中迭代的字符串的最后一个索引。以下是一个例子:

String words = "Apple gum fruit red orange triangle";
for(String foo : words.split("\\s+"){
    //How would I get the index of foo at any given point in the loop
 }

你可以用柜台吗

String words = "Apple gum fruit red orange triangle";
int counter = 0;
for(String foo : words.split("\\s+"){
    System.out.println(counter);
    counter++;
    //How would I get the index of foo at any given point in the loop
 }
如果你指的是起始字符的索引,你正在研究类似的东西

String words = "Apple gum fruit red orange triangle";
int index = 0;
for(String foo : words.split("\\s+"){
    System.out.println(index);
    index += foo.length() +1; // to dismiss the whitespace
    //How would I get the index of foo at any given point in the loop
 }

你可以用柜台吗

String words = "Apple gum fruit red orange triangle";
int counter = 0;
for(String foo : words.split("\\s+"){
    System.out.println(counter);
    counter++;
    //How would I get the index of foo at any given point in the loop
 }
如果你指的是起始字符的索引,你正在研究类似的东西

String words = "Apple gum fruit red orange triangle";
int index = 0;
for(String foo : words.split("\\s+"){
    System.out.println(index);
    index += foo.length() +1; // to dismiss the whitespace
    //How would I get the index of foo at any given point in the loop
 }
String语句=“苹果胶-水果-红-橙三角”;
字符串[]单词=句子。拆分(\\s+);
for(int i=0;i
字符串句子=“苹果胶水果红橙色三角形”;
字符串[]单词=句子。拆分(\\s+);
for(int i=0;i
单词数组中单词的索引或原始字符串中子字符串的索引是什么意思?不要使用
foreach
循环,而是使用
for
代替单词数组中单词的索引是什么意思,或者原始字符串中子字符串的索引?不要使用
foreach
循环,但是,
instedi的
将希望在迭代过程中的任何一点上访问foowords@ProgrammingCuber你在循环中有
foo
字段。这会让我得到文字的内容,还是只给我提供它在循环中的位置的索引?@ProgrammingCuber是文字。这不会给我苹果口香糖这个词果红色橙色三角形。它给出了它们的索引。我想在迭代过程中的任何一点上访问foowords@ProgrammingCuber你在循环中有
foo
字段。这会让我得到文字的内容,还是只给我它在循环中的位置的索引?@ProgrammingCuber是文字。这不会给我苹果胶水果红这个词橙色三角形。它给出了它们的索引。