2014-03-12 35 views
0

我定義了一個二維陣列和一個維度數組:VB-多維數組爲一名維陣列

Dim A2(,) As Decimal = New Decimal(1, 2) {{1, 2, 3}, {4, 5, 6}} 
    Dim A1(2) As Decimal 

現在我想A1的值設定爲A2的值(0) - ,其也2(實際上是3)Decimal的數組。

A1= A2(0) ' To insert {1,2,3} to A1 

我該怎麼寫vb?

我求佛我不能使用List,我的代碼必須使用這個簡單數組

回答

0

這段代碼的答案是:It can't be done。數組A1只能保存十進制值,而不能包含十進制值數組。

A1(0) = 123D ' Can hold a decimal value, not an array of decimals. 
A1(1) = 345D ' Can hold a decimal value, not an array of decimals. 
A1(2) = 854D ' Can hold a decimal value, not an array of decimals. 

對於這項工作,陣列A1必須被聲明爲數組的數組:

​​

現在可以分配陣列A1

A1(0) = {1D, 2D, 3D} 

Dim A2 As Decimal(,) = New Decimal((2 - 1), (3 - 1)) {{1, 2, 3}, {4, 5, 6}} 
Dim A1 As Decimal()() = New Decimal(2 - 1)() {} 

For i As Integer = 0 To (2 - 1) 
    Dim array As Decimal() = New Decimal(3 - 1) {} 
    For j As Integer = 0 To (3 - 1) 
     array(j) = A2(i, j) 
    Next 
    A1(i) = array 
Next 

MessageBox.Show(String.Join(Environment.NewLine, (From a As Decimal() In A1 Select String.Join(",", a)).ToArray())) 

輸出:

1,2,3 
4,5,6 
+0

OK,但我想插入A2(0,*)值 - 如何寫? A2(0)()或A2(0,*)不通過編譯 - 語法是什麼? – user2162278

+0

您必須循環遍歷所需的等級並自行構建陣列。 –

+0

@ user2162278我已經包含一個示例。請參閱我的更新。 –