如何在 Java 中为每个循环访问下一个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18872145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access the next element in for each loop in Java?
提问by Dilshad Abduwali
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?
我正在使用 for each 循环来访问字符串数组的每个元素并检查这些字符串的特定特征。如果当前元素显示了所需的字符,我需要访问此循环中的下一个元素。所以当前对我来说只是一个指标,表明下一个是我需要抓住和处理的。有没有办法存储当前的并处理正确的下一个?
thanks
谢谢
采纳答案by Peter Lawrey
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;
}
回答by Jeroen Vannevel
回答by MaVRoSCy
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
这将循环数组,如果满足条件,将打印相应的消息。在这种情况下,它将打印4
回答by AppX
You need to change if from a for-each-loop to a regular for-loop.
您需要将 if 从 for-each-loop 更改为常规 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];
}
}
回答by Ruchira Gayan Ranaweera
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;
}
}
回答by DPM
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.
` 玩得开心,如果有任何问题再次与我联系。
回答by Rok T.
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: