Iterate over a string array in Java

I have a String array with some components, this array has 5 components, and it changes several times. What I would like to do is iterate over this array and get the first component and the component next to it. So the first time I get component number one and component number 2, the second time I get the number 2 and 3, the third time I get the number 3 and 4 ... And so on, until you get to the last component.

This is how far I came:

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 do this?

+62
java collections arrays
Jul 15 '11 at 13:27
source share
9 answers

You can make an extended for loop (for Java 5 and above) to iterate through the elements of the array:

 String[] elements = {"a", "a", "a", "a"}; for (String s: elements) { //Do your stuff here System.out.println(s); } 
+151
Sep 11 '13 at 10:29 on
source share
 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 with [0,2] to get the elements 0,1 , 1,2 and 2,3 .

+34
Jul 15 '11 at 13:30
source share
 String current = elements[i]; if (i != elements.length - 1) { String next = elements[i+1]; } 

This ensures that you do not get an ArrayIndexOutOfBoundsException for the last element (there is no "next" there). Another option is to iterate to i < elements.length - 1 . It depends on your requirements.

+5
Jul 15 '11 at 13:29
source share
 String[] elements = { "a", "a","a","a" }; for( int i=0; i<elements.length-1; i++) { String s1 = elements[i]; String s2 = elements[i+1]; } 
+3
Jul 15 '11 at 13:32
source share

I would say instead of testing i less than elements.length - 1 testing i + 1 less than elements.length . You do not change the domain of the array that you are looking at (i.e., ignore the last element), but rather change the largest element that you are viewing at each iteration.

 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 } 
+2
Jul 15 2018-11-15T00:
source share

You must maintain consistency the number of times you access the array. Use it

 int lookUpTime=0; for(int i=lookUpTime;i<lookUpTime+2 && i<elements.length();i++) { // do something with elements[i] } lookUpTime++; 
+1
Jul 15 '11 at 13:48
source share

These algorithms are incorrect due to comparison:

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

or

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

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

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

or

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

or

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

Array ["a", "b"] will iterate as:

i = 0 represents <2: elements [0] give "a"

i = 1 represents <2: elements [1] give "b"

then exit the loop because 2 is not <2.

Wrong examples exit the loop prematurely and run only with the first element in this simple two-element case.

0
Feb 09 '17 at 19:53 on
source share
  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); } 
0
Mar 16 '18 at 7:43
source share

If you are looking for performance and the iteration order does not matter, you can iterate using an optimized back loop:

 for(int i=elements.lenth; --i>=0;) {...} 

Thus, you extract the length of the array once, then decrease the index and compare with zero in one atomic operation. Last, but not least, comparison with zero is a very fast operation often used by many processors (faster than comparison with array length).

0
Apr 16 '19 at 10:44
source share



All Articles