请解释for循环中发生的事情 [英] Please explain what is happening inside for loop

查看:83
本文介绍了请解释for循环中发生的事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class TwoDArray {public static void main(String args [])

{int twoD [] [] = new int [4] [5]; int i,j,k = 0;

for(i = 0; i< 4; i ++)

for(j = 0; j< 5; j ++)

{twoD [i] [j] = k; k ++;

}

for(i = 0; i< 4; i ++)

{for(j = 0; j< 5; j ++)

System.out.print(twoD [i] [j] +);的System.out.println(); }

}

}



我的尝试:



我明白我得到了什么输出,但我不知道我是怎么得到的。请让我知道循环内部发生了什么。

解决方案

首先,这里的代码更加可读,这使得它更容易解释:

  class  TwoDArray {
public 静态 void main( String args []){
int twoD [] [] = new int [ 4 ] [ 5 ];
int i,j,k = 0 ;
for (i = 0 ; i< 4 ; i ++)
for (j = 0 ; j< 5 ; j ++){
twoD [i] [j] = k;
k ++;
}
for (i = 0 ; i< 4 ; i ++){
for (j = 0 ; j< 5 ; j ++)
System.out.print(twoD [i] [j] + );
System.out.println();
}
}
}

完全有循环的四个(两个外部for循环,每个循环内部for循环)。

  for (i =  0 ; i<  4 ; i ++)
for (j = 0 ; j< 5 ; j ++){
twoD [i] [j] = k;
k ++;
}

以上是第一个外循环。在外部循环中运行的代码不包含在 {} 中,这意味着只有下一个语句将在循环中运行,这里这个语句是另一个循环!所以你有一个嵌套循环 [ ^ ],循环中的循环。循环运行时,变量 i j k会发生这种情况之前 k ++ )按时间顺序排列:

  • i = 0,j = 0,k = 0
  • i = 0,j = 1,k = 1
  • i = 0,j = 2,k = 2
  • i = 0,j = 3,k = 3
  • i = 0,j = 4,k = 4
  • i = 1,j = 0,k = 5
  • i = 1,j = 1,k = 6
  • i = 1,j = 2,k = 7
  • i = 1,j = 3,k = 8
  • i = 1,j = 4,k = 9
  • i = 2,j = 0,k = 10
  • ...依此类推。如您所见,对于每个 i j 的整个循环运行。




然后,第二个外循环:

  for (i =  0 ; i<  4 ; i ++){
< span class =code-keyword> for (j = 0 ; j< 5 ; j ++)
System.out.print(twoD [i] [j] + );
System.out.println();
}

同样的想法:它是一个嵌套循环。但是在这里,内部循环没有括号来指定循环内容的块,所以只有下一个语句属于循环,即 print 语句 - println 只属于外部循环,而不属于内部循环。


首先缩进代码,使其可读:

  class  TwoDArray {
public static void main( String args []){
int twoD [] [] = new int [ 4 ] [ 5 ];
int i,j,k = 0 ;
for (i = 0; i< 4; i ++)
for ( j = 0; j <5; j ++){
twoD [i] [j] = k;
k ++;
}
for (i = 0; i< 4; i ++){
for (j = 0; j< 5; j ++)
System.out.print(twoD [i] [j] + < span class =code-string>);
System.out.println();
}
}
}

现在你可以看到发生了什么:你有两个嵌套循环,一个索引 i 通过所有行,以及通过所有列索引 j 的内部行。

在内部循环中,设置每个单元格在数组中从递增序列的数字。因为内部循环适用于行中的所有列,所以每列的值都比前一列大,然后移动到下一行并继续使用它的列。


你不明白某些代码在做什么,使用调试器,它会告诉你。



有一个工具可以让你看到你的代码正在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器在这里向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个错误。



引用:

这段代码到底在做什么[i] [j] = k'?



与任何语言一样,你需要学习基础知识。


class TwoDArray { public static void main(String args[])
{ int twoD[][]= new int[4][5]; int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++)
{ twoD[i][j] = k; k++;
}
for(i=0; i<4; i++)
{ for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " "); System.out.println(); }
}
}

What I have tried:

I understood what output i am getting but i don't know how i am getting it. please let me know what is happening inside for loop.

解决方案

Firstly, here's the code in a bit more "readable" format, which makes it a bit easier to interpret:

class TwoDArray {
 public static void main(String args[]) {
  int twoD[][] = new int[4][5];
  int i, j, k = 0;
  for (i = 0; i < 4; i++)
   for (j = 0; j < 5; j++) {
    twoD[i][j] = k;
    k++;
   }
  for (i = 0; i < 4; i++) {
   for (j = 0; j < 5; j++)
    System.out.print(twoD[i][j] + " ");
   System.out.println();
  }
 }
}

You have four for loops totally (two outer for loops with each an inner for loop).

for (i = 0; i < 4; i++)
 for (j = 0; j < 5; j++) {
  twoD[i][j] = k;
  k++;
 }

The above is the first outer loop. The code that's ran in the outer loop is not wrapped within { } so that means that only the next statement will be ran in the loop, and here this statement is another loop! So you have a nested loop[^], a loop in a loop. When your loop runs, this is what will happen with the variables i, j and k (before k++) in chronological order:

  • i = 0, j = 0, k = 0
  • i = 0, j = 1, k = 1
  • i = 0, j = 2, k = 2
  • i = 0, j = 3, k = 3
  • i = 0, j = 4, k = 4
  • i = 1, j = 0, k = 5
  • i = 1, j = 1, k = 6
  • i = 1, j = 2, k = 7
  • i = 1, j = 3, k = 8
  • i = 1, j = 4, k = 9
  • i = 2, j = 0, k = 10
  • ... and so on. As you can see, for each i, the whole loop for j runs.


Then, the second outer loop:

for (i = 0; i < 4; i++) {
 for (j = 0; j < 5; j++)
  System.out.print(twoD[i][j] + " ");
 System.out.println();
}

Same idea: it's a nested loop. But here, the inner loop doesn't have brackets to specify the block for the loop 'contents', so only the next statement belongs to the loop, which is the print statement - the println only belongs to the outer loop, and not to the inner loop.


Start by indenting your code so it's readable:

class TwoDArray { 
    public static void main(String args[]){
        int twoD[][]= new int[4][5]; 
        int i, j, k = 0;
        for(i=0; i<4; i++) 
            for(j=0; j<5; j++){ 
                twoD[i][j] = k; 
                k++;
                }
        for(i=0; i<4; i++){ 
            for(j=0; j<5; j++) 
                System.out.print(twoD[i][j] + " "); 
            System.out.println(); 
            }
        }
    }

Now you can see what is going on: you have two nested loops, one which which indexes i through all rows, and an inner one which indexes j through all columns.
Inside the inner loop, you set each cell in the array to a number from an incrementing sequence. Because the inner loop works on all the columns in a row, each column gets a value one greater than its predecessor, and then you move to the next row and continue with it's columns.


When you don't understand that some code is doing, use the debugger, it will show you.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Quote:

what exactly this code is doing'twod[i][j]=k' ?


Like for any language, you need to learn the basics.


这篇关于请解释for循环中发生的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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