了解C#和Java上传统和增强迭代的实现

Nan*_*ada 0 c# java compiler-construction

我感觉很困惑的方式或者C#foreachJava的增强的作品,甚至更令人沮丧的是明白为什么我还没有碰到过这个细节来之前.

但无论如何,事实上,我真的很想理解为什么这个看似相似的流控制语句的工作方式如此不同.为了便于说明,我们假设我们需要迭代一个整数数组,两个实现都是这样的:

C#4.0(代码)

class Program
{
    public static void Main(string[] args)
    {
        int[] foobar = new int[] {0, 1, 1, 2, 3, 5, 8, 13, 21};
        Console.WriteLine(String.Format("[DEBUG] type: {0}", foobar.GetType()));
        Console.WriteLine(String.Format("[DEBUG] length: {0}", foobar.Length));
        try
        {
            for (int i = 0; i < foobar.Length; i++)
            {
                Console.Write(String.Format("{0} ", foobar[i]));
            }
            Console.Write(Environment.NewLine);

            foreach (var i in foobar) {
                Console.Write(String.Format("{0} ", foobar[i]));
            }
        }
        catch (Exception exception)
        {
            Console.Write(Environment.NewLine);
            Console.WriteLine(exception.ToString());
        }
        finally
        {
            Console.Write(Environment.NewLine);
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

C#4.0(输出)

[DEBUG] type: System.Int32[]
[DEBUG] length: 9
0 1 1 2 3 5 8 13 21
0 1 1 1 2 5 21
System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Dotnet.Samples.Sandbox.Program.Main(String[] args) in e:\My Dropbox\Work\P
rojects\scm\git\sandbox\Dotnet.Samples.Sandbox\Dotnet.Samples.Sandbox\Program.cs
:line 51

Press any key to continue . . .
Run Code Online (Sandbox Code Playgroud)

JAVA SE6(代码)

class Program {
    public static void main(String[] args) {
        int[] foobar = new int[] {0, 1, 1, 2, 3, 5, 8, 13, 21};
        System.out.println("[DEBUG] type: " + (foobar.getClass().isArray() ? "Array " : "") +  foobar.getClass().getComponentType());
        System.out.println("[DEBUG] length: " + foobar.length);
        try {    
            for (int i = 0; i < foobar.length; i++)
            {
                System.out.print(String.format("%d ", foobar[i]));
            }
            System.out.print(System.getProperty("line.separator"));
            for (int i : foobar) {
                System.out.print(String.format("%d ", foobar[i]));
            }
        } catch (Exception e) {
            System.out.print(System.getProperty("line.separator"));
            System.out.println(e.toString());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

JAVA SE6(输出)

[DEBUG] type: Array int
[DEBUG] length: 9
0 1 1 2 3 5 8 13 21 
0 1 1 1 2 5 21 
java.lang.ArrayIndexOutOfBoundsException: 13
Run Code Online (Sandbox Code Playgroud)

Car*_*000 8

在C#版本..

foreach (var i in foobar)
{
    Console.Write(String.Format("{0} ", foobar[i]));
}
Run Code Online (Sandbox Code Playgroud)

..应该..

foreach (var i in foobar)
{
    Console.Write(String.Format("{0} ", i));
}
Run Code Online (Sandbox Code Playgroud)

foreach按原样执行一个整数数组,不会迭代数组索引:它遍历整数.

鉴于阵列..

int[] foobar = new int[] {0, 1, 1, 2, 3, 5, 8, 13, 21};
Run Code Online (Sandbox Code Playgroud)

..你的代码是:

Printing element 0: 0
Printing element 1: 1
Printing element 1: 1
Printing element 2: 1
Printing element 3: 2
Printing element 5: 5
Printing element 8: 21
Printing element 13: IndexOutOfRangeException !!
Run Code Online (Sandbox Code Playgroud)