2016-08-19 85 views
-4

我有这个类在C#中阵列阵列的数组C#

public static class Fases 
{ 
    public static int [,,] fase1 = new int[, , ] { 
     {{1},{1 ,3}}, 
     {{2},{2, 2, 2}, {2, 2, 2 }}, 
     {{2}, {3, 1, 1, 1}, {3, 1, 1, 1}} 
    }; 
} 

当我做

Fases.fase1[0, 1, 1] 

它抛出IndexOutOfRangeException

谢谢!

+6

你不应该访问像'Fases.fase1数组[0] [1] [1] '? – Andrew

+3

此代码不能编译。 –

+0

这不会编译。请参阅[多维数组初始化](https://msdn.microsoft.com/en-IN/library/2yd9wwz4.aspx) –

回答

1

你有什么不是一个数组数组,它​​是一个3维数组。多维数组必须具有统一的布局,由于内部数组的长度不同,您的代码将无法编译。

为了得到阵列的数组的数组代码将需要

using System; 

public class Program 
{ 
    public static void Main() 
    { 
     var result = Fases.fase1[0][1][1]; 
     Console.WriteLine(result); 
    } 
} 

public static class Fases 
{ 
    public static int [][][] fase1 = new int[][][] { 
     new int [][] {new int[] {1}, new int[] {1 ,3}}, 
     new int [][] {new int[] {2}, new int[] {2, 2, 2}, new int[] {2, 2, 2 }}, 
     new int [][] {new int[] {2}, new int[] {3, 1, 1, 1}, new int[] {3, 1, 1, 1}} 
    }; 
} 

which compiles and runs

+0

请注意,内部数组类型可由编译器推断。例如:'new int [] [] {new [] {1},new [] {1,3}}}' –