在Java中遍历字符串数组

时间:2023-01-23 17:15:39

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.

我有一些组件的字符串数组,这个数组有5个组件,它有时会变化。我要做的是遍历这个数组,得到第一个组件和旁边的组件。所以第一次得到第一个分量和第2个分量,第二次得到2和3,第三次是3和4…一直到最后一个分量。

This how far I have come:

我已经走了这么远:

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?

我怎样才能做到这一点呢?

8 个解决方案

#1


131  

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:

为了增加上面或下面的答案,您可以对数组元素的迭代执行增强的for循环(用于java 5或更高版本):

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

#2


29  

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.

注意,在这种情况下,元素。长度是4,所以要从[0,2]迭代得到元素0,1,1,2,2,3。

#3


3  

String current = elements[i];
if (i != elements.length - 1) {
   String next = elements[i+1];
}

This makes sure you don't get an ArrayIndexOutOfBoundsException for the last element (there is no 'next' there). The other option is to iterate to i < elements.length - 1. It depends on your requirements.

这确保您不会获得最后一个元素的ArrayIndexOutOfBoundsException(这里没有“next”)。另一个选项是迭代i <元素。长度- 1。这取决于你的要求。< p>

#4


3  

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

for( int i=0; i<elements.length-1; i++)
{
    String s1 = elements[i];
    String s2 = elements[i+1];
}

#5


1  

I would argue instead of testing i less than elements.length - 1 testing i + 1 less than elements.length. You aren't changing the domain of the array that you are looking at (i.e. ignoring the last element), but rather changing the greatest element you are looking at in each iteration.

我宁愿争论,而不是测试I小于元素。长度- 1测试i + 1小于元素。长度。您没有更改正在查看的数组的域(即忽略最后一个元素),而是更改在每次迭代中查看的最大元素。

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

for(int i = 0; i + 1 < elements.length; i++) {
    String first = elements[i];
    String second = elements[i+1];
    //do something with the two strings
}

#6


1  

You have to maintain the serial how many times you are accessing the array.Use like this

您必须维护串行,即访问数组的次数。使用这样的

int lookUpTime=0;

    for(int i=lookUpTime;i<lookUpTime+2 && i<elements.length();i++)
     {
    // do something with elements[i]
    }

lookUpTime++;

#7


0  

Those algorithms are both incorrect because of the comparison:

这些算法都是不正确的,因为比较:

for( int i = 0; i < elements.length - 1; i++)

for(int i = 0;我 <元素。长度- 1;我+ +)< p>

or

for(int i = 0; i + 1 < elements.length; i++) {

for(int i = 0;i + 1 < elements.length;我+ +){

It's true that the array elements range from 0 to length - 1, but the comparison in that case should be less than or equal to. Those should be:

确实,数组元素的范围是从0到长度- 1,但是这种情况下的比较应该小于或等于。这些应该是:

for(int i = 0; i < elements.length; i++) {

for(int i = 0;我< elements.length;我+ +){

or

for(int i = 0; i <= elements.length - 1; i++) {

for(int i = 0;我< =元素。长度- 1;我+ +){

or

for(int i = 0; i + 1 <= elements.length; i++) {

for(int i = 0;i + 1 <=元素。长度;我+ +){

The array ["a", "b"] would iterate as:

数组["a", "b"]将迭代为:

i = 0 is < 2: elements[0] yields "a"

i = 0 = < 2:元素[0]产生“a”

i = 1 is < 2: elements[1] yields "b"

i = 1 = < 2:元素[1]产生“b”

then exit the loop because 2 is not < 2.

然后退出循环,因为2不小于2。

The incorrect examples both exit the loop prematurely and only execute with the first element in this simple case of two elements.

不正确的示例都提前退出循环,并且只在两个元素的简单情况下使用第一个元素执行。

#8


0  

    String[] nameArray= {"John", "Paul", "Ringo", "George"};
    int numberOfItems = nameArray.length;
    for (int i=0; i<numberOfItems; i++)
    {
        String name = nameArray[i];
        System.out.println("Hello " + name);
    }

#1


131  

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:

为了增加上面或下面的答案,您可以对数组元素的迭代执行增强的for循环(用于java 5或更高版本):

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

#2


29  

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.

注意,在这种情况下,元素。长度是4,所以要从[0,2]迭代得到元素0,1,1,2,2,3。

#3


3  

String current = elements[i];
if (i != elements.length - 1) {
   String next = elements[i+1];
}

This makes sure you don't get an ArrayIndexOutOfBoundsException for the last element (there is no 'next' there). The other option is to iterate to i < elements.length - 1. It depends on your requirements.

这确保您不会获得最后一个元素的ArrayIndexOutOfBoundsException(这里没有“next”)。另一个选项是迭代i <元素。长度- 1。这取决于你的要求。< p>

#4


3  

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

for( int i=0; i<elements.length-1; i++)
{
    String s1 = elements[i];
    String s2 = elements[i+1];
}

#5


1  

I would argue instead of testing i less than elements.length - 1 testing i + 1 less than elements.length. You aren't changing the domain of the array that you are looking at (i.e. ignoring the last element), but rather changing the greatest element you are looking at in each iteration.

我宁愿争论,而不是测试I小于元素。长度- 1测试i + 1小于元素。长度。您没有更改正在查看的数组的域(即忽略最后一个元素),而是更改在每次迭代中查看的最大元素。

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

for(int i = 0; i + 1 < elements.length; i++) {
    String first = elements[i];
    String second = elements[i+1];
    //do something with the two strings
}

#6


1  

You have to maintain the serial how many times you are accessing the array.Use like this

您必须维护串行,即访问数组的次数。使用这样的

int lookUpTime=0;

    for(int i=lookUpTime;i<lookUpTime+2 && i<elements.length();i++)
     {
    // do something with elements[i]
    }

lookUpTime++;

#7


0  

Those algorithms are both incorrect because of the comparison:

这些算法都是不正确的,因为比较:

for( int i = 0; i < elements.length - 1; i++)

for(int i = 0;我 <元素。长度- 1;我+ +)< p>

or

for(int i = 0; i + 1 < elements.length; i++) {

for(int i = 0;i + 1 < elements.length;我+ +){

It's true that the array elements range from 0 to length - 1, but the comparison in that case should be less than or equal to. Those should be:

确实,数组元素的范围是从0到长度- 1,但是这种情况下的比较应该小于或等于。这些应该是:

for(int i = 0; i < elements.length; i++) {

for(int i = 0;我< elements.length;我+ +){

or

for(int i = 0; i <= elements.length - 1; i++) {

for(int i = 0;我< =元素。长度- 1;我+ +){

or

for(int i = 0; i + 1 <= elements.length; i++) {

for(int i = 0;i + 1 <=元素。长度;我+ +){

The array ["a", "b"] would iterate as:

数组["a", "b"]将迭代为:

i = 0 is < 2: elements[0] yields "a"

i = 0 = < 2:元素[0]产生“a”

i = 1 is < 2: elements[1] yields "b"

i = 1 = < 2:元素[1]产生“b”

then exit the loop because 2 is not < 2.

然后退出循环,因为2不小于2。

The incorrect examples both exit the loop prematurely and only execute with the first element in this simple case of two elements.

不正确的示例都提前退出循环,并且只在两个元素的简单情况下使用第一个元素执行。

#8


0  

    String[] nameArray= {"John", "Paul", "Ringo", "George"};
    int numberOfItems = nameArray.length;
    for (int i=0; i<numberOfItems; i++)
    {
        String name = nameArray[i];
        System.out.println("Hello " + name);
    }