国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

C#數(shù)組

2019-11-11 02:58:22
字體:
來源:轉載
供稿:網(wǎng)友

1.數(shù)組的概念

數(shù)組是一個存儲相同類型元素的固定大小的順序集合。

所有的數(shù)據(jù)都是由連續(xù)的內(nèi)存位置組成

可以通過索引來訪問數(shù)組中的元素

2.數(shù)組的聲明和初始化

(1)聲明

datatype指數(shù)組的類型,[]可以指定數(shù)組的緯度,arrayName指數(shù)組的名稱

datatype[] arrayName;

(2)初始化

數(shù)組是一個引用類型,需要使用 new 關鍵字來創(chuàng)建數(shù)組的實例

string [] Students=new string[3]

(3)賦值給數(shù)組

string[] Students = new string[3] { "kaven", "melon", "lucy" };

(4)遍歷數(shù)組

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 數(shù)組{ class PRogram { static void Main(string[] args) { string[] Students = new string[3] { "kaven", "melon", "lucy" }; //for (int i = 0; i < Students.Length; i++) //{ // Console.WriteLine(Students[i]); //} Console.WriteLine("學生列表:"); Console.WriteLine("++++++++++++++++++++++++++"); foreach (string s in Students) { Console.WriteLine(s); } Console.WriteLine("++++++++++++++++++++++++++"); //通過索引訪問數(shù)組元素 Console.WriteLine("第三個學生是:" + Students[2]); Console.ReadKey(); } }}

這里寫圖片描述

3.二維數(shù)組

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 數(shù)組{ class Program { static void Main(string[] args) { int[,] number = new int[5, 2]{ {0,0}, {1,1}, {2,4}, {3,9}, {4,16} }; Console.WriteLine("++++++++++++++++++"); for (int i = 0; i < 5; i++) { for (int j = 0; j < 2; j++) { Console.WriteLine("第{0}行{1}列為{2}",i,j,number[i,j]); } } Console.WriteLine("++++++++++++++++++"); Console.ReadKey(); } }}

這里寫圖片描述

4.數(shù)組參數(shù)

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 數(shù)組{ class Program { public static int getMultiple(int[] arr) { int result = 1; foreach (int item in arr) { result = result * item; } return result; } static void Main(string[] args) { // 求乘積 int[] number = new int[5] { 1, 3, 5, 7, 9 }; Console.Write("數(shù)組元素"); foreach (int i in number) { Console.Write(i+"、"); } Console.Write("的乘積是"); Console.Write(getMultiple(number)); Console.ReadKey(); } }}

這里寫圖片描述

5.參數(shù)數(shù)組

參數(shù)數(shù)組通常用于傳遞未知數(shù)量的參數(shù)給函數(shù)。

格式

public 返回類型 方法名稱( params 類型名稱[] 數(shù)組名稱 )using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 數(shù)組{ class Program { public static double getSum(params double[] arr) { double sum = 0; for (int i = 0; i < arr.Length; i++) { sum += arr[i]; } return sum; } static void Main(string[] args) { Console.WriteLine(getSum(1.2, 2.3, 3.4, 4.5)); Console.ReadKey(); } }}

結果

這里寫圖片描述

把 params關鍵字去掉就會報錯

這里寫圖片描述

6.Array

Array 類提供了各種用于數(shù)組的屬性和方法,是所有數(shù)組的基類

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 數(shù)組{ class Program { static void Main(string[] args) int[] a1 = { 1, 2, 3, 4, 5, 6, 7, 8 }; int[] a2=new int[5]; //數(shù)組a1從第一個元素開始復制5個元素到數(shù)組a2 Array.Copy(a1, a2, 5); Console.WriteLine("數(shù)組a2:"); foreach (int i in a2) { Console.Write(i+" "); } Console.WriteLine(); // int[] original = new int[] { 78, 12, 39, 90, 64, 56, 30, 2, 7 }; int[] temp = original; //原始數(shù)組 Console.WriteLine("原始數(shù)組"); foreach (int i in original) { Console.Write(i + " "); } Console.WriteLine(); //逆轉數(shù)組 Array.Reverse(temp); Console.WriteLine("逆轉數(shù)組"); foreach (int i in temp) { Console.Write(i + " "); } Console.WriteLine(); //排序數(shù)組 Array.Sort(temp); Console.WriteLine("排序數(shù)組"); foreach (int i in temp) { Console.Write(i + " "); } Console.ReadKey(); } }}

這里寫圖片描述


上一篇:背包問題

下一篇:zcmu1866——Sumsets

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 湘潭县| 武定县| 阳城县| 宽甸| 新邵县| 池州市| 吴旗县| 芦山县| 云南省| 清涧县| 平江县| 常德市| 黔南| 吉林省| 疏附县| 昂仁县| 九江市| 台东县| 高陵县| 阳江市| 乡城县| 离岛区| 化隆| 临西县| 来宾市| 林西县| 定陶县| 高州市| 怀化市| 靖安县| 射阳县| 西藏| 淄博市| 东阳市| 若羌县| 乌拉特中旗| 伊川县| 巨野县| 临江市| 侯马市| 峨边|