Question

I am using a for each loop to visit each element of an array of Strings and checking specific characteristics of those Strings. I need to access the next element in this loop if the current one has shown the character desired. So the current on is just an indicator for me that the next one is the one I need to grap and process. Is there any way to store the current one and process the right next one?

thanks

Was it helpful?

Solution

You either need to use an indexed loop.

for(int i=0;i<strings.length-1;i++) {
    String curr = strings[i];
    String next = strings[i+1];
}

or you need to compare the current to the previous not the next.

String curr = null;
for(String next: strings) {
    if (curr != null) {
        // compare
    }
    curr = next;
}

OTHER TIPS

You can try something like this

    String valBefore=new String();
    boolean flag=false;
    for (String i:str){
         if(i.equals("valueBeforeTheExpectedValue")){
             valBefore=i;
             flag=true;
             continue;
         } if (flag){
             // Now you are getting expected value
             // while valBefore has previous value 
             flag=false;
        }
    }

You can try like this:

String myArray[]= { "this","is","the","value"};
......
int counter=0;
for(String x:myArray){
  counter++;
  if(x.equals("value")){
    System.out.println(counter);
  }
}

This will loop the array, and if the condition is met, the appropriate message will print. In this case it will print 4

You can use continue; to skip the current iteration and proceed with the next one. Save the value in a variable to use it.

You need to change if from a for-each-loop to a regular for-loop.

String[] strings = new String[]{"aaa","bbb","ccc"};

for (int i = 0; i < strings.length; i++) {
    if (strings[i].equals("aaa")) {
        i++;
        String anotherValue = strings[i];
    }
}

Not elegant, but might work for some things. Add extra element to the list:

    List<String> stringsOnList= new ArrayList<String>(Arrays.asList(new String[]{ "1" , "2", "3" }));
    String currentString = null;
    stringsOnList.add(""); // add extra element for the extra loop
    for(String nextString : stringsOnList){
        if(currentString==null){
            currentString=nextString;
            continue;
        }
        //Do stuff e.g.: {
        System.out.println("Current:"+currentString+", next: "+nextString);
        // } 

        currentString=nextString; // current is next
    }

Output:

Current:1, next: 2
Current:2, next: 3
Current:3, next: 

According to ur need i have made a sample code here...

public class One {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String stringArray[]={"Dinakar","DinaNath"};
        for(String singleString:stringArray)
        {
            if(singleString.charAt(0)=='D')
            {
                // process with singleString here
                System.out.print("Got it ");//my work done
                break;
            }       
        }
    }
}

Have fun if any probs again contact me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top