对于循环工作,但For Each不。为什么? [英] For loop works, but For Each does not. Why?

查看:127
本文介绍了对于循环工作,但For Each不。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在包含forEach循环的行上抛出异常异常,但据我所知,此代码没有任何问题。 for循环从char数组的元素0开始并循环直到它到达最后一个元素...但是当我使用更长的for循环尝试此代码时,即



($ i $ 0; i< nested.length; i ++)

代码按预期工作。



为什么for循环工作且forEach循环在这种情况下不起作用?

  public static void main(String [] args){
String S =hello;
String curly1 ={;
String box1 =[;
String p1 =(;
String curly2 =};
String box2 =];
String p2 =);
char [] nested = S.toCharArray();
int flag = 0;

if(nested [0]!= curly1.charAt(0))flag = 0;
if(nested [nested.length-1]!= curly2.charAt(0))flag = 0;

for(char i:nested){
if(nested [i] == curly1.charAt(0)&& nested [(i + 1)] == box1。 charAt(0)|| nested [i] == box1.charAt(0)&& nested [(i + 1)] == p1.charAt(0)){
flag = 1; }
else if(nested [i] == p2.charAt(0)&& nested [(i + 1)] == box2.charAt(0)|| nested [i] == box2。 charAt(0)&& nested [(i + 1)] == curly2.charAt(0)){
flag = 1; }
else {flag = 0;}
}
System.out.println(flag);

}
}


解决方案

如果您需要在循环使用中使用索引访问某些内容,而不是foreach(enhaced for)。



现在使用变量 i 嵌套数组>类型 char 。变量 i 表示正在迭代的嵌套数组的元素。因此,如果使用此变量访问数组,则其值将隐式转换为其int表示形式(例如'a'== 97 ),这将导致异常。



在你的情况下,你需要for循环,或者将当前索引值保存在其他变量中,并在每次迭代时递增它,因为你不能这样做具有增强的索引算术:嵌套[i + 1]


There is an outofbounds exception being thrown on the line containing the forEach loop, but as far as I can see, there is nothing wrong with this code. The for loop is starting with element 0 of the char array and looping through until it reaches the last element... But when I try this code using a longer for loop, i.e.,

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

the code works as expected.

Why does the for loop work and the forEach loop not work in this situation?

  public static void main(String[] args) {
      String S = "hello";
      String curly1 = "{";
      String box1 = "[";
      String p1 = "(";
      String curly2 = "}";
      String box2 = "]";
      String p2 = ")";
               char[] nested = S.toCharArray();
        int flag = 0;

        if(nested[0] != curly1.charAt(0)) flag = 0;
        if(nested[nested.length-1] != curly2.charAt(0)) flag = 0;

        for(char i : nested) {
            if(nested[i] == curly1.charAt(0) && nested[(i+1)] == box1.charAt(0) || nested[i] == box1.charAt(0) && nested[(i+1)] == p1.charAt(0)) {
                flag = 1; }
             else if(nested[i] == p2.charAt(0) && nested[(i+1)] == box2.charAt(0) || nested[i] == box2.charAt(0) && nested[(i+1)] == curly2.charAt(0)) {
                 flag = 1; }
                else { flag = 0;}
        }
        System.out.println(flag);

    }
}

解决方案

If you need to access something using index in your loop use for, not foreach (enhaced for).

Now you are accessing the nested array using variable i of type char. The variable i represents an element of the nested array that is being iterated over. So if you access the array using this variable, its value will be implicitly converted to its int representation (for instance 'a' == 97), which will cause the exception.

In your case you need the for loop, or to keep current index value in some other variable and increment it with each iteration, because you can't do this sort of index arithmetics with enhanced for: nested[i + 1]

这篇关于对于循环工作,但For Each不。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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