mirror of
https://github.com/krahets/hello-algo.git
synced 2025-01-23 22:40:25 +08:00
Update csharp array to unify code style,and create its unit test.
This commit is contained in:
parent
5c999939f6
commit
aa868cfa3d
@ -1,14 +1,12 @@
|
||||
/*
|
||||
* File: Array.cs
|
||||
* Created Time: 2022-12-14
|
||||
* Author: mingXta (1195669834@qq.com)
|
||||
*/
|
||||
// File: Array.cs
|
||||
// Created Time: 2022-12-14
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist
|
||||
{
|
||||
public class Array
|
||||
{
|
||||
/* 随机返回一个数组元素 */
|
||||
//随机返回一个数组元素
|
||||
public static int RandomAccess(int[] nums)
|
||||
{
|
||||
Random random = new();
|
||||
@ -17,7 +15,7 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* 扩展数组长度 */
|
||||
//扩展数组长度
|
||||
public static int[] Extend(int[] nums, int enlarge)
|
||||
{
|
||||
// 初始化一个扩展长度后的数组
|
||||
@ -31,7 +29,7 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
//在数组的索引 index 处插入元素 num
|
||||
public static void Insert(int[] nums, int num, int index)
|
||||
{
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
@ -43,7 +41,7 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
//删除索引 index 处元素
|
||||
public static void Remove(int[] nums, int index)
|
||||
{
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
@ -53,7 +51,7 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
}
|
||||
}
|
||||
|
||||
/* 遍历数组 */
|
||||
//遍历数组
|
||||
public static void Traverse(int[] nums)
|
||||
{
|
||||
int count = 0;
|
||||
@ -69,7 +67,7 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
}
|
||||
}
|
||||
|
||||
/* 在数组中查找指定元素 */
|
||||
//在数组中查找指定元素
|
||||
public static int Find(int[] nums, int target)
|
||||
{
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
@ -80,43 +78,10 @@ namespace hello_algo.chapter_array_and_linkedlist
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*辅助函数,数组转字符串 */
|
||||
//辅助函数,数组转字符串
|
||||
public static string ToString(int[] nums)
|
||||
{
|
||||
return string.Join(",", nums);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void Main()
|
||||
{
|
||||
/* 初始化数组 */
|
||||
int[] arr = new int[5];
|
||||
Console.WriteLine("数组 arr = " + ToString(arr));
|
||||
int[] nums = { 1, 3, 2, 5, 4 };
|
||||
Console.WriteLine("数组 nums = " + ToString(nums));
|
||||
|
||||
/* 随机访问 */
|
||||
int randomNum = RandomAccess(nums);
|
||||
Console.WriteLine("在 nums 中获取随机元素 " + randomNum);
|
||||
|
||||
/* 长度扩展 */
|
||||
nums = Extend(nums, 3);
|
||||
Console.WriteLine("将数组长度扩展至 8 ,得到 nums = " + ToString(nums));
|
||||
|
||||
/* 插入元素 */
|
||||
Insert(nums, 6, 3);
|
||||
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + ToString(nums));
|
||||
|
||||
/* 删除元素 */
|
||||
Remove(nums, 2);
|
||||
Console.WriteLine("删除索引 2 处的元素,得到 nums = " + ToString(nums));
|
||||
|
||||
/* 遍历数组 */
|
||||
Traverse(nums);
|
||||
|
||||
/* 查找元素 */
|
||||
int index = Find(nums, 3);
|
||||
Console.WriteLine("在 nums 中查找元素 3 ,得到索引 = " + index);
|
||||
}
|
||||
}
|
||||
}
|
70
codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs
Normal file
70
codes/csharp/test/chapter_array_and_linkedlist/ArrayTest.cs
Normal file
@ -0,0 +1,70 @@
|
||||
// File: ArrayTest.cs
|
||||
// Created Time: 2022-12-16
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
using NUnit.Framework;
|
||||
using Array = hello_algo.chapter_array_and_linkedlist.Array;
|
||||
|
||||
namespace hello_algo.Test.chapter_array_and_linkedlist
|
||||
{
|
||||
[TestFixture]
|
||||
internal class ArrayTest
|
||||
{
|
||||
private int[] nums;
|
||||
|
||||
[SetUp]
|
||||
public void setup()
|
||||
{
|
||||
//初始化数组
|
||||
nums = new int[] { 1, 3, 2, 5, 4 };
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestRandomAccess()
|
||||
{
|
||||
//随机访问
|
||||
int randomNum = Array.RandomAccess(nums);
|
||||
Console.WriteLine($"在 nums 中获取随机元素 {randomNum}");
|
||||
Assert.Contains(randomNum, nums);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestExtend()
|
||||
{
|
||||
//长度扩展
|
||||
int[] target = { 1, 3, 2, 5, 4, 0, 0, 0 };
|
||||
nums = Array.Extend(nums, 3);
|
||||
Console.WriteLine($"将数组长度扩展至 8 ,得到 nums = {Array.ToString(nums)}");
|
||||
Assert.AreEqual(target, nums);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestInsert()
|
||||
{
|
||||
//插入元素
|
||||
int[] target = { 1, 3, 2, 6, 5 };
|
||||
Array.Insert(nums, 6, 3);
|
||||
Console.WriteLine($"在索引 3 处插入数字 6 ,得到 nums = {Array.ToString(nums)}");
|
||||
Assert.AreEqual(target, nums);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestRemove()
|
||||
{
|
||||
//删除元素
|
||||
int[] target = { 1, 3, 5, 4, 4 };
|
||||
Array.Remove(nums, 2);
|
||||
Console.WriteLine($"删除索引 2 处的元素,得到 nums = {Array.ToString(nums)}");
|
||||
Assert.AreEqual(target, nums);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestFind()
|
||||
{
|
||||
//查找元素
|
||||
int index = Array.Find(nums, 3);
|
||||
Console.WriteLine("在 nums 中查找元素 3 , 得到索引 = " + index);
|
||||
Assert.AreEqual(1, index);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user