Array, two-dimensional array, multi-dimensional array, jagged array

1. What is an array (one-dimensional array)?

  • The so-called array is an ordered sequence of elements.
  • An array is a collection used to store multiple data of the same type.
  • The array is a contiguous memory space.
  • Several elements of the same type are organized in an unordered form, and the set of these unordered elements of the same type is called an array.
  • Ordinary arrays that are generally used are called one-dimensional arrays.

Second, the basic structure of the array

1. Basic declaration form

//基本结构:变量类型[]变量名 = new 变量类型[数组长度]{元素,元素,元素};

int[]Arr = new int[5]; //只设定初始数组长度的申明形式。

int[]Arr = new int[]{1,2,3,4,5,6}; //只设定元素数量的申明形式,数组长度会自动匹配。

int[]Arr = new int[5] {1,2,3,4,5};//设定了数组长度和元素数量的申明形式。

Three, the relevant characteristics of the array

  • The elements in the array are arranged in disorder, but the array has the concept of subscripts, and the specific subscript elements can be found by searching the subscripts.
  • The array subscripts are arranged in an orderly manner, and the position and arrangement order of the subscripts cannot be modified.
  • The array identifier is a reference used to point to an object created in the heap, and this object holds references to other objects.
  • The types of the elements in the array must be the same type.
  • The Length in the array can only indicate the maximum length of the array, but not the number of elements actually saved.
  • The array must be assigned to the element before it can be used.

Four, two-dimensional array

What is a two-dimensional array?

  • A two-dimensional array is essentially an array with arrays as array elements. That is, "array in array".
  • Array is an extension of ordinary one-dimensional array and has the same characteristics as one-dimensional array.

The basic structure of a two-dimensional array:

//基本结构:变量类型[,]变量名 = new 变量类型[行数 , 列数]{
   
   {列表1},{列表2}};
 int[,] Arr = new int[2, 3];//只设定初始数组行数和列数的申明形式。
int[,] Arr = new int[2,3] { {1,2,3 },{4,5,6 } };//设定了数组行数和列数的元素数量的申明形式。
int[,] Arr = new int[,] { {1,2,3 },{4,5,6 } }; //只设定行数和列数的申明形式,数组列数和行数会自动匹配。

Five, multi-dimensional array

What is a multidimensional array?

  • Multi-dimensional array refers to the name of an array in three dimensions and above.
  • Multi-dimensional arrays have at least 3 concepts, and concepts such as rows, columns, and layers (the concept is just a name).
  • Multi-dimensional arrays have the same characteristics as one-dimensional arrays.
  • The dimensions of a multidimensional array can be increased indefinitely.

The basic structure of a multidimensional array:

//在二维数组的基础上进行多次嵌套就可实现多维数组。
int[,,] Arr = new int[,,] { { { 0},{0 } },{ {0 },{0 } },{ {0 },{0 } } };//多维数组样例1(三维)
int[,,,] Arr = new int[,,,] { { { { 1,13},{ 2,14},{3,15 } } },{ { {4,16 },{5,17 },{ 6,18} } },{ { {7,19 },{8,20 },{9,21 } } },{ { { 10,22},{ 11,23},{ 12,24} } } };//多维数组样例2(四维)

Six, interleaved/sawtooth array

What is an interleaved/jagged array?

  • Both jagged arrays and two-dimensional arrays are called "arrays in arrays".
  • In multi-dimensional and two-dimensional arrays, they can set dimensions and place elements. The jagged array can be placed as one-dimensional or multi-dimensional arrays as elements.
  • That is, each element in the jagged array is an array.

The basic structure of the interleaved/sawtooth array:

 //int[交错数组的维度][放置数组的维度] Arr
 int[][] Arr = new int[1][] { new int[] { 1, 2, 3 } }; //一维的交错数组中放置一维数组。
 int[][,] arr = new int[][,] { new int[,] { { 2, 2 }, { 1, 2 } }, new int[,] { { 1, 2, 3 }, { 1, 3, 4 } } };//一维交错放置二维数组。
 int[,][,] arr = new int[,][,]{
   
   {new int[,]{
   
   {1,2},{1,2}}, new int[,]{
   
   {1,2,3},{1,3,4}}},{new int[,]{
   
   {2,2},{1,2}}, new int[,]{
   
   {1,2,3},{1,3,4}}}};//二维交错数组放置二维数组

Seven, array related functions

  • Rank, get the dimension of the array.
  • LongLength, get the sum of all elements in the array, and return the long type.
  • Length, get the sum of all elements in the array, and return the int type.
  • BinarySearch, dichotomy finds the element, returns the subscript after the query is found, and returns a negative number if the query fails
  • Clear, delete all elements in the specified range.
  • ConstrainedCopy, copies the elements in the specified range in the array to the specified position in another array.
  • Copy, copy the specified element in the array to another array.
  • IndexOf, finds the specified element and returns the element index.
  • LastIndexOf, find the element from the back and return the index (when there are multiple identical elements in the array, return the index of the next element).
  • Resize, change the length of the array.
  • Reverse, reverse the elements in the specified range and exchange positions.
  • Sort, which automatically sorts the array.
  • CopyTo, copy all the elements of an array to the specified position in another array.
  • GetValue, get the element of the specified subscript in the array.
  • SetValue, changes the element of the specified subscript of the array.
  • Contains, to determine whether the array contains the specified element.
  • Sum, get the sum of all elements in the array.
  • Max, get the element with the largest value in the array.
  • Min, get the element with the smallest value in the array.
  • Average, get the average value of all elements in the array.
  • Count, get the length of the array (only one-dimensional arrays are available).

Note: This record is only for my own understanding, if there are any errors, please correct me.

Guess you like

Origin blog.csdn.net/weixin_45213735/article/details/95331552