Iterate through string array in Java

后端 未结 10 929
逝去的感伤
逝去的感伤 2020-11-30 01:21

I have String array with some components, this array has 5 components and it vary some times. What I would like to do is to iterate through that array and get the first comp

10条回答
  •  余生分开走
    2020-11-30 01:59

    String[] elements = { "a", "a", "a", "a" };
    
    for( int i = 0; i < elements.length - 1; i++)
    {
        String element = elements[i];
        String nextElement = elements[i+1];
    }
    

    Note that in this case, elements.length is 4, so you want to iterate from [0,2] to get elements 0,1, 1,2 and 2,3.

提交回复
热议问题