1
- /*
2
- * File: Array.cs
3
- * Created Time: 2022-12-14
4
- * Author: mingXta ([email protected] )
5
- */
6
-
1
+ // File: Array.cs
2
+ // Created Time: 2022-12-14
3
+ // Author: mingXta ([email protected] )
4
+
7
5
namespace hello_algo . chapter_array_and_linkedlist
8
6
{
9
7
public class Array
10
8
{
11
- /* 随机返回一个数组元素 */
9
+ //随机返回一个数组元素
12
10
public static int RandomAccess ( int [ ] nums )
13
11
{
14
12
Random random = new ( ) ;
@@ -17,7 +15,7 @@ public static int RandomAccess(int[] nums)
17
15
return randomNum ;
18
16
}
19
17
20
- /* 扩展数组长度 */
18
+ //扩展数组长度
21
19
public static int [ ] Extend ( int [ ] nums , int enlarge )
22
20
{
23
21
// 初始化一个扩展长度后的数组
@@ -31,7 +29,7 @@ public static int[] Extend(int[] nums, int enlarge)
31
29
return res ;
32
30
}
33
31
34
- /* 在数组的索引 index 处插入元素 num */
32
+ // 在数组的索引 index 处插入元素 num
35
33
public static void Insert ( int [ ] nums , int num , int index )
36
34
{
37
35
// 把索引 index 以及之后的所有元素向后移动一位
@@ -43,7 +41,7 @@ public static void Insert(int[] nums, int num, int index)
43
41
nums [ index ] = num ;
44
42
}
45
43
46
- /* 删除索引 index 处元素 */
44
+ // 删除索引 index 处元素
47
45
public static void Remove ( int [ ] nums , int index )
48
46
{
49
47
// 把索引 index 之后的所有元素向前移动一位
@@ -53,7 +51,7 @@ public static void Remove(int[] nums, int index)
53
51
}
54
52
}
55
53
56
- /* 遍历数组 */
54
+ //遍历数组
57
55
public static void Traverse ( int [ ] nums )
58
56
{
59
57
int count = 0 ;
@@ -69,7 +67,7 @@ public static void Traverse(int[] nums)
69
67
}
70
68
}
71
69
72
- /* 在数组中查找指定元素 */
70
+ //在数组中查找指定元素
73
71
public static int Find ( int [ ] nums , int target )
74
72
{
75
73
for ( int i = 0 ; i < nums . Length ; i ++ )
@@ -80,43 +78,10 @@ public static int Find(int[] nums, int target)
80
78
return - 1 ;
81
79
}
82
80
83
- /* 辅助函数,数组转字符串 */
81
+ // 辅助函数,数组转字符串
84
82
public static string ToString ( int [ ] nums )
85
83
{
86
84
return string . Join ( "," , nums ) ;
87
85
}
88
-
89
- /* Driver Code */
90
- public static void Main ( )
91
- {
92
- /* 初始化数组 */
93
- int [ ] arr = new int [ 5 ] ;
94
- Console . WriteLine ( "数组 arr = " + ToString ( arr ) ) ;
95
- int [ ] nums = { 1 , 3 , 2 , 5 , 4 } ;
96
- Console . WriteLine ( "数组 nums = " + ToString ( nums ) ) ;
97
-
98
- /* 随机访问 */
99
- int randomNum = RandomAccess ( nums ) ;
100
- Console . WriteLine ( "在 nums 中获取随机元素 " + randomNum ) ;
101
-
102
- /* 长度扩展 */
103
- nums = Extend ( nums , 3 ) ;
104
- Console . WriteLine ( "将数组长度扩展至 8 ,得到 nums = " + ToString ( nums ) ) ;
105
-
106
- /* 插入元素 */
107
- Insert ( nums , 6 , 3 ) ;
108
- Console . WriteLine ( "在索引 3 处插入数字 6 ,得到 nums = " + ToString ( nums ) ) ;
109
-
110
- /* 删除元素 */
111
- Remove ( nums , 2 ) ;
112
- Console . WriteLine ( "删除索引 2 处的元素,得到 nums = " + ToString ( nums ) ) ;
113
-
114
- /* 遍历数组 */
115
- Traverse ( nums ) ;
116
-
117
- /* 查找元素 */
118
- int index = Find ( nums , 3 ) ;
119
- Console . WriteLine ( "在 nums 中查找元素 3 ,得到索引 = " + index ) ;
120
- }
121
86
}
122
- }
87
+ }
0 commit comments