通过在Java中的字符串数组迭代 [英] Iterate through string array in Java

查看:215
本文介绍了通过在Java中的字符串数组迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些部件String数组,这个数组有5个组成部分,它的一些变化倍。我想这样做的是通过数组进行迭代,并获得第一个组件和组件旁边之一。所以第一次我会得到部分头号和组件号2,第二次会得到数2和3,第三次3号和4 ...等,直到你得到的最后一个组件。

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 component and the component next to that one. So the first time I would get the component number one and the component number 2, the second time would get the number 2 and 3, the third time number 3 and 4... And so on until you get to the last component.

这我已经走了多远:

String[] elements = { "a", "a","a","a" };

for( int i = 0; i <= elements.length - 1; i++)
{
    // get element number 0 and 1 and put it in a variable, 
    // and the next time get element      1 and 2 and put this in another variable. 
}

我怎样才能做到这一点?

How can I accomplish this?

推荐答案

只需添加到答案的上方或下方,你可以做一个的增强的for循环(对Java 5及更高版本)迭代上数组的元素:

Just to add to the answers above or below that you could do an Enhanced For-Loop (for java 5 and higher) for iteration on array's elements:

String[] elements = { "a","a","a","a" };   
for (String s: elements) {           
        //Do your stuff here
        System.out.println(s); 
    }

这篇关于通过在Java中的字符串数组迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆