namespace TriangleCalc { class Program { static void Main(string[] args) { int[][] dataArry = new int[5][]; dataArry[0]=new int[1]; dataArry[0][0]=7; dataArry[1]=new int[2]; dataArry[1][0]=3;dataArry[1][1]=8; dataArry[2]=new int[3]; dataArry[2][0]=8;dataArry[2][1]=1;dataArry[2][2]=0; dataArry[3]=new int[4]; dataArry[3][0]=2;dataArry[3][1]=7;dataArry[3][2]=4;dataArry[3][3]=4; dataArry[4]=new int[5]; dataArry[4][0]=4;dataArry[4][1]=5;dataArry[4][2]=2;dataArry[4][3]=6;dataArry[4][4]=6; for (int i=0;i<dataArry.Length;i++) { int[] line=dataArry[i]; for (int j=0;j<line.Length;j++) { Console.Write("{0},",line[j]); } Console.WriteLine(); } } } }
C#二維數組初始化的方法與示例
http://www.sowsoy.com/topics-549.html
C#二維數組定義並初始化語法結構
變量類型[,] 數組名 = new 變量類型[一維元素個數,二維元素個數]{{元素00,元素01,元素02...},{元素10,元素11,元素12...}...}
如定義一個二維數組(第一緯長度為2,第二緯長度為3)並初始化:
int[,] a2=new int[,]{{1,2,3},{4,5,6}};
C#數組簡介
---------------------------------------
// 定義一個一維數組
int[] array1 = new int[5];
// 定義一個一維數組並賦值
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
// 替代語法
int[] array3 = { 1, 2, 3, 4, 5, 6 };
// 定義一個二維數組
int[,] multiDimensionalArray1 = new int[2, 3];
// 定義二維數組並賦值: 注意兩個數組的個數要
int[,] multiDimensionalArray2 = { { 1, 2, 3 },{ 4, 5, 6} };
// 定一一個交錯數組
int[][] jaggedArray =int[][] jaggedArray = new int[6][];
// 可定義可變長度的數
jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
jaggedArray[1] = new int[] { 1, 2, 3 };
-----------------------------------------
數組具有以下屬性:
數組可以是一維、多維或交錯的。
數值數組元素的默認值設置為零,而引用元素的默認值設置為 null。
交錯數組是數組的數組,因此,它的元素是引用類型,初始化為 null。
數組的索引從零開始:具有 n 個元素的數組的索引是從 0 到 n-1。
數組元素可以是任何類型,包括數組類型。
數組類型是從抽象基類型 Array 派生的引用類型。由於此類型實現了 IEnumerable 和 IEnumerable<(Of <(T>)>),因此可以對 C# 中的所有數組使用 foreach 迭代。
C#二維數組的兩種初始化方法
方法1:假設一個初始長度,在對數組添加元素的時候,你都需要對數組做邊界檢查,如果數組尺寸不夠了,你需要創建一個新的數組,然后復制原來的數組到新的數組。
方法2:初始化第一維長度為0,在每次插入新的原始的時候,都復制一個新的數組,使它的長度等於原來數組長度+1,這里假定第二維是不變的。
1、打印一個[4,6]長度的二維數組:
using System;
class Matrix
{
static void Main()
{
int[,] arr=new int[4,6];
for(int i=0;i<4;i++)
{
for(int j=0;j<6;j++)
{
arr[i,j]=(i+1)*10+j+1;
}
}
for(int i=0;i<4;i++)
{
for(int j=0;j<6;j++)
{
Console.Write("{0} ",arr[i,j]);
}
Console.WriteLine();
}
}
}
2、二維數組給Datatable賦值
[STAThread]
static void Main(string[] args)
{
string [,] arrStr={
{"1","No1"},
{"2","No2"},
{"3","No3"},
{"4","No4"}
};
DataTable dtTable = new DataTable("testdt");
DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "Col1";
myDataColumn.Caption = "Col1";
dtTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType("System.String");
myDataColumn.ColumnName = "Col2";
myDataColumn.Caption = "Col2";
dtTable.Columns.Add(myDataColumn);
//賦值
for (int i=0;i<4;i++)
{
DataRow drRow = dtTable.NewRow();
for (int j=0;j<2;j++)
{
Console.Write("{0}、",arrStr[i,j]);
drRow[j] = arrStr[i,j];
}
dtTable.Rows.Add(drRow);
dtTable.AcceptChanges();
Console.WriteLine();
}
//讀數據
for (int i=0;i<dtTable.Rows.Count;i++)
{
for (int j=0;j<dtTable.Columns.Count;j++)
{
Console.Write("{0}、",dtTable.Rows[i][j]);
}
Console.WriteLine();
}
Console.ReadLine();
}
二維數組的初始化的方法
1、分行初紿化
int a[2][5]={{1,3,5,7,9},{2,4,6,8,10}};
這種賦值方法比較直觀,把第一個花括號內的數據給第一行的元素,第二個花括號內的數據給第二行的元素,
2、不分行初始化
可以將所有數據寫在一個花括號內,按數組排列的順序對個元素賦初值。如:int a[2][5]={1,3,5,7,9,2,4,6,8,10};
3、部分初始化
int a[2][5]={{1,3,5},{2,4,6}};
其余元素值自動為0。賦初值后數組各元素為:
1 3 5 0 0
2 4 6 0 0
4、省略行數
對數組中的全體元素都賦初值時,二維數組的定義中第一維的長度也可以省略,但二維的長度不能省略。如:
int a[2][3]={1,3,5,7,9,11};
等價於:int a[][3]={1,3,5,7,9,11};
但不能寫成:int a[2][]={1,3,5,7,9,11};
在分行初始化時,由於給出的初值已清楚的表明了行數和各行中元素的個數,因此第一維的大小可以不定義。如:int b[][3]={{1},{0,2},{3,2,1}};顯然這是一個三行三列的數組,其各元素的值為:
1 0 0
0 2 0
3 2 1
二維數組中的數據是按行形式存放在內存中。