哪个 For 循环会先执行,并且先结束?

huangapple go评论84阅读模式
英文:

Which For loop will execute first, and finish first?

问题

我对嵌套循环的变体感到困惑。不知何故,这些循环给出了相同的结果,但我无法理解。比如,在第二段代码中,它在 col < values[rows] 部分说,values[rows] 不是应该等于 4(因为有 4 行吗)?所以我只是想知道这些循环是如何交互的,哪个先执行,哪个先完成?

 for (int row = 0; row &lt; 4; row++)                                    
 {
    for (int col = 0; col &lt; 3; col++)
    {
       System.out.print(values[row][col] + &quot; &quot;);
    }

    System.out.println();   
}
for (int row = 0; row &lt; values.length; row++)
{
   for (int col = 0; col &lt; values[row].length; col++)
   {
      System.out.print(values[row][col] + &quot; &quot;);
   }

   System.out.println();    
}
英文:

Im confused over the nested loop variant. Somehow these loops give the same result, but I cant wrap my head around it. like, in the second code, it says in the part of col < values[rows], wouldnt the values[rows] = 4(since there are 4 rows)? So im just wondering how does these loops interact and which one executes first and finishes first?

 for (int row = 0; row &lt; 4; row++)                                    
 {
    for (int col = 0; col &lt; 3; col++)
    {
       System.out.print(values[row][col] + &quot; &quot;);
    }

    System.out.println();   
}
for (int row = 0; row &lt; values.length; row++)
{
   for (int col = 0; col &lt; values[row].length; col++)
   {
      System.out.print(values[row][col] + &quot; &quot;);
   }

   System.out.println();    
}

答案1

得分: 4

以下是翻译好的内容:

我不确定您在这里尝试询问什么,但我猜您只是想了解这两个代码块之间的区别。

我将通过一个简单的示例来为您解释:

import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][]values = new int[n][m];
        for(int i=0; i<n;i++){
            for(int j=0; j<m; j++){                //读取一个 5 x 8 的数组
                values[i][j]= sc.nextInt();
            }
        }
        System.out.print("输入数组:");
        for(int i=0; i<n;i++){
            for(int j=0; j<m; j++){
                System.out.print(values[i][j] + " ");
            }
            System.out.println("");
        }
        System.out.print("第一个代码块:");
        for (int row = 0; row < 4; row++)                                    
        {
             for (int col = 0; col < 3; col++)
             {
                  System.out.print(values[row][col] + " ");
             }
            System.out.println();   
        }
        System.out.print("第二个代码块:");    
        for (int row = 0; row < values.length; row++)
        {
            for (int col = 0; col < values[row].length; col++)
            {
                 System.out.print(values[row][col] + " ");
            }

            System.out.println();    
         }    
     }
}

输入:

5 8 
3 3 3 3 3 3 3 3 
4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 
6 6 6 6 6 6 6 6 
7 7 7 7 7 7 7 7 

程序的输出为:

输入数组:3 3 3 3 3 3 3 3 
4 4 4 4 4 4 4 4 
5 5 5 5 5 5 5 5 
6 6 6 6 6 6 6 6 
7 7 7 7 7 7 7 7 
第一个代码块:3 3 3 
4 4 4 
5 5 5 
6 6 6 
第二个代码块:3 3 3 3 3 3 3 3 
4 4 4 4 4 4 4 4 
5 5 5 5 5 5 5 5 
6 6 6 6 6 6 6 6 
7 7 7 7 7 7 7 7 

因此,正如您所见,如果 values.length = 4values[row].length = 3,则第一个代码块的工作方式与第二个代码块完全相同;否则,它们将产生不同的结果。在遍历数组时,良好的做法是使用 array.length 而不是硬编码长度的值。

英文:

I am not sure what are you trying to ask here but I guess you just wish to know the difference between the two block of codes.

I'll explain that to you with an simple example:

import java.util.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][]values = new int[n][m];
for(int i=0; i&lt;n;i++){
for(int j=0; j&lt;m; j++){                //read a 5 x 8 array
values[i][j]= sc.nextInt();
}
}
System.out.print(&quot;Input Array :&quot;);
for(int i=0; i&lt;n;i++){
for(int j=0; j&lt;m; j++){
System.out.print(values[i][j] +&quot; &quot;);
}
System.out.println(&quot;&quot;);
}
System.out.print(&quot;First Code :&quot;);
for (int row = 0; row &lt; 4; row++)                                    
{
for (int col = 0; col &lt; 3; col++)
{
System.out.print(values[row][col] + &quot; &quot;);
}
System.out.println();   
}
System.out.print(&quot;Second Code :&quot;);    
for (int row = 0; row &lt; values.length; row++)
{
for (int col = 0; col &lt; values[row].length; col++)
{
System.out.print(values[row][col] + &quot; &quot;);
}
System.out.println();    
}    
}
}

Input:

5 8 
3 3 3 3 3 3 3 3 
4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 
6 6 6 6 6 6 6 6 
7 7 7 7 7 7 7 7 

Output of the program is:

Input Array :3 3 3 3 3 3 3 3 
4 4 4 4 4 4 4 4 
5 5 5 5 5 5 5 5 
6 6 6 6 6 6 6 6 
7 7 7 7 7 7 7 7 
First Code :3 3 3 
4 4 4 
5 5 5 
6 6 6 
Second Code :3 3 3 3 3 3 3 3 
4 4 4 4 4 4 4 4 
5 5 5 5 5 5 5 5 
6 6 6 6 6 6 6 6 
7 7 7 7 7 7 7 7 

So, as you can see first block works exactly like second block if values.length =4 and values[row].length = 3 otherwise they will give different results. To traverse an array good practice is to go with array.length instead of hard coding the value of length.

答案2

得分: 2

在第一个循环中:

System.out.print(values[0][0] + " ");

System.out.print(values[0][1] + " ");

System.out.print(values[0][2] + " ");

System.out.print(values[1][0] + " ");

System.out.print(values[1][1] + " ");

System.out.print(values[1][2] + " ");

.......

以此类推... 内部循环将运行4次,因为行数(rows)的值为4。所以内部循环(列循环)的输出为 4 * 3 = 12 个 System.out.print()。明白吗?行数乘以列数就是你要找的方程。在计算第二段代码时要用到这个方程。为了更好地帮助你,请展示在 values 参数中定义的值。

英文:

In the firs loop:

System.out.print(values[0][0] + " ");

System.out.print(values[0][1] + " ");

System.out.print(values[0][2] + " ");

System.out.print(values[1][0] + " ");

System.out.print(values[1][1] + " ");

System.out.print(values[1][2] + " ");

.......

and so on.... The intern loop will run 4x, since the value of rows = 4. So the output of the intern loop (the col loop) is 4 * 3 = 12 System.out.print(). Get it? Row * Col is the equation that you are looking for. Use it to calc the second code. To help more please show the values defined in values param.

答案3

得分: 1

在你的第一个部分中的 for 循环中,你正试图迭代一个 4 x 3 的数组。所以对于每一行,它会迭代通过指定的列数。它会停在最后一行的最后一列。

对于你的第二个部分中的 for 循环,我认为当你不知道行和列的具体值时,这样做可能更合适。

第一个情况取决于第二种情况中行的长度,但在第一个情况中是固定的。

英文:

For the For loops in your first section, you are trying to iterate through a 4 x 3 array. So for each row it iterates through the number of columns specified. It stops at the last column of the last row.

For the for loops in your second section, I think it would be more appropriate when you don't know the values for rows and columns.

The first to finish depends on the length of the row in the second case but is fixed on the first one

huangapple
  • 本文由 发表于 2020年3月15日 07:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/60688295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定