[英]Array of Arrays in C#
我知道存在多維數組,但我認為在我的情況下我不需要它! 我試過這段代碼..但是不知道怎么用初始化列表初始化..
double [] [] a = new double [2] []; // = {{1,2},{3,4}};
謝謝
PS:如果你想知道我為什么使用它:我需要數據結構,當我調用obj [0]它返回一個數組..我知道它很奇怪..
謝謝
Afaik,最簡單和按鍵有效的方法是初始化鋸齒狀數組是:
double[][] x = new []{new[]{1d, 2d}, new[]{3d, 4.3d}};
編輯:
實際上這也有效:
double[][] x = {new[]{1d, 2d}, new[]{3d, 4.3d}};
這應該工作:
double[][] a = new double[][]
{
new double[] {1.0d, 2.0d},
new double[] {3.0d, 4.0d}
};
由於您有一個數組數組,您還必須在其中創建數組對象:
double[][] a = new double[][] {
new double[] { 1, 2 },
new double[] { 3, 4 }
};
double[][] a = new double[][] {
new double[] {1.0, 1.0},
new double[] {1.0, 1.0}
};
我不知道我是否對此感到滿意,但我一直在VB.net中使用所謂的Structures,並想知道如何在C#中看到這個概念。 它以這種方式與這個問題相關:
' The declaration part
Public Structure driveInfo
Public type As String
Public size As Long
End Structure
Public Structure systemInfo
Public cPU As String
Public memory As Long
Public diskDrives() As driveInfo
Public purchaseDate As Date
End Structure
' this is the implementation part
Dim allSystems(100) As systemInfo
ReDim allSystems(1).diskDrives(3)
allSystems(1).diskDrives(0).type = "Floppy"
看看這一切是多么優雅,並且比鋸齒狀陣列更容易訪問。 如何在C#中完成所有這些(結構可能?)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.