c# 2维数组 取一维_C#| 不同类型的一维数组声明
c# 2維數組 取一維
In the below example, we are declaring an integer array (one dimensional) with following styles:
在下面的示例中,我們聲明具有以下樣式的整數數組(一維) :
1) One dimensional Array declaration with initialization (without array size)
1)具有初始化的一維數組聲明(無數組大小)
Example:
例:
int[] arr1 = { 12, 45, 56, 78, 89 };In this style, arr1 will automatic occupy the size of the array based on given elements.
在這種樣式中, arr1將根據給定的元素自動占據數組的大小。
2) Dynamic One dimensional Array declaration with fixed size
2)具有固定大小的動態一維數組聲明
Example:
例:
int[] arr2 = new int[5];In this style, arr2 will occupy the size for 5 integers dynamically.
在這種樣式中, arr2將動態占用5個整數的大小。
3) Dynamic One dimensional Array declaration with variable size
3)具有可變大小的動態一維數組聲明
Example:
例:
int[] arr3 = new int[size];In this style, we will read the size of the arr3 first and then declare the array dynamically.
在這種樣式中,我們將先讀取arr3的大小,然后動態聲明數組。
Example: In the below given example – we are using these 3 styles to declare an array.
示例:在下面給出的示例中–我們使用這3種樣式聲明一個數組。
.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}Program:
程序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace Arrays {class Program{static void Main(string[] args){//Single DimensionConsole.WriteLine("Type 1 : Declaration");int[] arr1 = { 12, 45, 56, 78, 89 };foreach(int item in arr1){Console.Write("{0}\t", item);}Console.WriteLine("\n\nType 2 : Declaration");int[] arr2 = new int[5];Console.WriteLine("Enter 5 Values:");for(int i=0;i<arr2.Length;i++){arr2[i] = Convert.ToInt32(Console.ReadLine());}foreach (int item in arr2){Console.Write("{0}\t", item);}Console.WriteLine("\n\nType 3 : Declaration");Console.Write("Enter Size:");int size = Convert.ToInt32(Console.ReadLine());int[] arr3 = new int[size];Console.WriteLine("Enter {0} Values:",size);for (int i = 0; i < arr3.Length; i++){arr3[i] = Convert.ToInt32(Console.ReadLine());}foreach (int item in arr3){Console.Write("{0}\t", item);}Console.ReadKey();}} }Output
輸出量
Type 1 : Declaration 12 45 56 78 89Type 2 : Declaration Enter 5 Values: 10 20 30 40 50 10 20 30 40 50Type 3 : Declaration Enter Size:3 Enter 3 Values: 100 200 300 100 200 300翻譯自: https://www.includehelp.com/dot-net/different-types-of-one-dimensional-array-declarations.aspx
c# 2維數組 取一維
總結
以上是生活随笔為你收集整理的c# 2维数组 取一维_C#| 不同类型的一维数组声明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c ++查找字符串_C ++类和对象|
- 下一篇: 微擎pc 导入前缀_段覆盖前缀| 808