最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net C# array数组操作
时间:2022-06-25 08:32:08 编辑:袖梨 来源:一聚教程网
数据是每一款开发应用程序都会用的,他是一ojb数据类型 哦,下面我们来看看关于.net数组吧,下面来看看一款简单扼要应用实例。
using system;
using system.collections.generic;
using system.text;namespace funwitharrays
{
class program
{
static void main(string[] args)
{
console.writeline("***** fun with arrays *****");
simplearrays();
arrayinitialization();
arrayofobjects();
rectmultidimensionalarray();
jaggedmultidimensionalarray();
passandreceivearrays();
systemarrayfunctionality();
console.readline();
}#region helper methods
static void simplearrays()
{
console.writeline("=> simple array creation.");
// create and fill an array of 3 integers
int[] myints = new int[3];
myints[0] = 100;
myints[1] = 200;
myints[2] = 300;// now print each value.
foreach (int i in myints)
console.writeline(i);
console.writeline();
}static void arrayinitialization()
{
console.writeline("=> array initialization.");
// array initialization syntax using the new keyword.
string[] stringarray = new string[] { "one", "two", "three" };
console.writeline("stringarray has {0} elements", stringarray.length);// array initialization syntax without using the new keyword.
bool[] boolarray = { false, false, true };
console.writeline("boolarray has {0} elements", boolarray.length);// array initialization with new keyword and size.
int[] intarray = new int[4] { 20, 22, 23, 0 };
console.writeline("intarray has {0} elements", intarray.length);
console.writeline();
}static void arrayofobjects()
{
console.writeline("=> array of objects.");// an array of objects can be anything at all.
object[] myobjects = new object[4];
myobjects[0] = 10;
myobjects[1] = false;
myobjects[2] = new datetime(1969, 3, 24);
myobjects[3] = "form & void";foreach (object obj in myobjects)
{
// print the type and value for each item in array.
console.writeline("type: {0}, value: {1}", obj.gettype(), obj);
}
console.writeline();
}
static void rectmultidimensionalarray()
{
console.writeline("=> rectangular multidimensional array.");
// a rectangular md array.
int[,] mymatrix;
mymatrix = new int[6, 6];// populate (6 * 6) array.
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++)
mymatrix[i, j] = i * j;// print (6 * 6) array.
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
console.write(mymatrix[i, j] + "t");
console.writeline();
}
console.writeline();
}static void jaggedmultidimensionalarray()
{
//锯齿形多维数组
console.writeline("=> jagged multidimensional array.");
// a jagged md array (i.e., an array of arrays).
// here we have an array of 5 different arrays.
int[][] myjagarray = new int[5][];// create the jagged array.
for (int i = 0; i < myjagarray.length; i++)
myjagarray[i] = new int[i + 7];// print each row (remember, each element is defaulted to zero!)
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < myjagarray[i].length; j++)
console.write(myjagarray[i][j] + " ");
console.writeline();
}
console.writeline();
}static void printarray(int[] myints)
{
for (int i = 0; i < myints.length; i++)
console.writeline("item {0} is {1}", i, myints[i]);
}static string[] getstringarray()
{
string[] thestrings = { "hello", "from", "getstringarray" };
return thestrings;
}static void passandreceivearrays()
{
console.writeline("=> arrays as params and return values.");
int[] ages = { 20, 22, 23, 0 };
printarray(ages);
string[] strs = getstringarray();
foreach (string s in strs)
console.writeline(s);
console.writeline();
}static void systemarrayfunctionality()
{
console.writeline("=> working with system.array.");
// initialize items at startup.
string[] gothicbands = { "tones on tail", "bauhaus", "sisters of mercy" };// print out names in declared order.
console.writeline(" -> here is the array:");
//数组上界getupperbound
for (int i = 0; i <= gothicbands.getupperbound(0); i++)
{
// print a name
console.write(gothicbands[i] + " ");
}
console.writeline("n");// reverse them...
// 反转数组
array.reverse(gothicbands);
console.writeline(" -> the reversed array");
// ... and print them.
for (int i = 0; i <= gothicbands.getupperbound(0); i++)
{
// print a name
console.write(gothicbands[i] + " ");
}
console.writeline("n");// clear out all but the final member.
console.writeline(" -> cleared out all but one...");
array.clear(gothicbands, 1, 2);
for (int i = 0; i <= gothicbands.getupperbound(0); i++)
{
// print a name
console.write(gothicbands[i] + " ");
}
console.writeline();
}
#endregion
}
}
相关文章
- 云星穹铁道权益升级怎么玩 云崩铁权益升级活动介绍 12-03
- 以闪亮之名山中绘梦怎么玩 以闪亮之名滢然花梦套装活动介绍 12-03
- 以闪亮之名绿野梦歌礼包怎么样 以闪亮之名绿野梦歌拍照礼包介绍 12-03
- 光遇12.6有什么更新 光遇12月6日更新内容介绍 12-03
- 伍六七暗影交锋测试资格怎么获得 伍六七暗影交锋内测资格获取方法 12-03
- 无限暖暖晶莹诗集怎么样 12-03