Get the App
SLTechnology News&Howtos  ›  Development  › 

Analysis of examples of C # Array Operation

Shulou Source: shulou.com Published: 2022-06-02 09:16:08 10月02日 Update

This article mainly explains the "example Analysis of C # Array Operation". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought slowly and deeply. Let's study and learn "C # array operation example analysis"!

An array is a collection of objects of the same type. Because arrays can be of almost any length, you can use arrays to store thousands or even millions of objects, but you must determine the size of the array when you create it. Each item in the array is accessed by index, which is a number indicating where or where the object is stored in the array. Arrays can be used to store both reference types and value types.

C # array operation program:

Using System

Using System.Collections.Generic

Using System.Text

Namespace ClassAboutArray

{

Public class CreateArray

{

/ / /

/ / definition of one-dimensional array

/ / /

Public void testArr1 ()

{

Int [] myIntArr = new int [100]

/ / define an int array with a length of 100

String [] mystringArr = new string [100]

/ / define a string array with a length of 100

Object [] myObjectArr = new object [100]

/ / define an int array with a length of 100

Int [] myIntArr2 = new int [] {1,2,3}

/ / define an int array of length 3

String [] mystringArr2 = new string [] {"oil", "salt"}

/ / define a string array of length 2

}

/ / /

/ / definition of multidimensional array

/ / /

Public void testArr2 ()

{

Int [,] myIntArr = new int [10,100]

/ / define a two-dimensional int array of 10 to 100

String [,] mystringArr = new string [2,2,3]

/ / define a 3-D string array of 2'2'3

Int [,] myIntArr2 = new int [,] {{1,2,3}, {- 1,-2,-3}}

/ / define a two-dimensional int array of 2x3 and initialize the

String [,] mystringArr2 = new string [,] {{"oil", "salt"}, {"besieged fortress", "morning dew"}}

/ / define a two-dimensional string array of 2x2 and initialize the

}

/ / /

/ / definition of staggered array

/ / /

Public void testArr3 ()

{

Int [] [] myJaggedArray = new int [3] []

MyJaggedArray [0] = new int [5]

MyJaggedArray [1] = new int [4]

MyJaggedArray [2] = new int [2]

Int [] myJaggedArray2 = new int [] []

{

New int [] {1,3,5,7,9}

New int [] {0,2,4,6}

New int [] {11,22}

}

}

}

Public class TraverseArray

{

/ / /

/ / use GetLowerBound | GetUpperBound to traverse the array

/ / /

Public void test1 ()

{

/ / define two-dimensional array

String [,] myStrArr2 = new string [,]

{{"oil", "salt"}, {"besieged city", "morning dew"}, {"hairy bear", "Snoopy"}}

/ / Loop output

For (int I = myStrArr2.GetLowerBound (0); I 3-> 2-> 1->

Console.WriteLine ("original array:")

For (int I = 0; I 2-> 3-> 4-> 5->

Console.WriteLine ("sort the following array:")

For (int I = 0; I Li Si (4)-> Wang Wu (3)-> pockmarked (2)-> naughty (1)->

Console.WriteLine ("original array:")

For (int I = 0; I pockmarked (2)-> Wang Wu (3)-> Li Si (4)-> Zhang San (5)

Console.WriteLine ("sort the following array:")

For (int I = 0; I 4-> 3-> 2-> 1->

Console.WriteLine ("original array:")

For (int I = 0; I 2-> 3-> 4-> 5->

Console.WriteLine ("reverse subsequent array:")

For (int I = 0; I 4-> 3-> 2-> 1->

Console.WriteLine ("original array:")

For (int I = 0; I 4-> 3->

Console.WriteLine ("copy array:")

For (int I = 0; I 4-> 3-> 2-> 1->

Console.WriteLine ("original array:")

For (int I = 0; I 0-> 5-> 4-> 3-> 2-> 1->

Console.WriteLine ("copy array:")

For (int I = 0; I

Tags: Array primitive output sort length Category method Wang Wu two-dimensional Analysis naughty object pockmarked Zhang San Li Si Storage besieged City Morning dew Type Learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Xiaomi MariaDB NVidia Linux