C#中如何申请动态数组

本文介绍两种在C#中创建并排序数组的方法:一种是一次性输入由逗号分隔的数字字符串,另一种是使用List<double>动态添加数字直至开始排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文出处:https://zhidao.baidu.com/question/1603714274790064067.html

【提问】
我想申请一个数组,自己输入数,然后对数组进行排序,求大神教如果可以把排序的方法也说了吧

最佳答案
用List就可以了,List<类型> list=new List<类型>();
就可以动态的添加删除了。
如果需要转化为数组,类型[] array=list.ToArray();就可以了。

其他回答

C#中并没有你想要的不定长数组,我给你两个解决方案:

第一种是让用户一次性输入一个用“,”分隔的字符串,如“1,2,3,4,5”,然后后台根据元素的个数确定数组的长度:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Console.WriteLine( "Please input numbers in format (1,2,3,4,...100)" );
             var  input = Console.ReadLine();
             var  strings = input.Split( ',' );
             var  numbers =  new  double [strings.Length];
 
             for  ( int  i = 0; i < strings.Length; i++)
             {
                 numbers[i] = Convert.ToDouble(strings[i]);
             }
 
             bool  flag =  true ;
             double  temp;
             int  numLength = numbers.Length;
             //sorting an array
             for  ( int  i = 1; (i <= (numLength - 1)) && flag; i++)
             {
                 flag =  false ;
                 for  ( int  j = 0; j < (numLength - 1); j++)
                 {
                     if  (numbers[j + 1] > numbers[j])
                     {
                         temp = numbers[j];
                         numbers[j] = numbers[j + 1];
                         numbers[j + 1] = temp;
                         flag =  true ;
                     }
                 }
             }
 
             Console.WriteLine( string .Join( "," , numbers));

第二种方法可以用List<double>来实现动态加载新的元素:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
      var  input =  string .Empty;
 
             var  numberList =  new  List< double >();
 
             while  (!input.Equals( "Sort" ))
             {
                 Console.WriteLine( "Please input a number, input \"Sort\" to start sorting the array." );
                 input = Console.ReadLine();
 
                 if  (!input.Equals( "Sort" ))
                 {
                     numberList.Add(Convert.ToDouble(input));
                 }
             }
 
             Console.WriteLine( "Original array: {0}" string .Join( "," , numberList));
 
             numberList.Sort( new  DoubleComparer());
 
             Console.WriteLine( "Sorted array: {0}" string .Join( "," , numberList));
 
             Console.WriteLine( "Sorting" );

其中的DoubleComparer的定义为:

1
2
3
4
5
6
7
class  DoubleComparer : IComparer< double >
         {
             public  int  Compare( double  x,  double  y)
             {
                 return  x > y ? -1 : x == y ? 0 : 1;
             }
         }

C#中,动态数组是一种可以在运行时调整大小的数组结构。与传统的固定大小数组不同,动态数组允许在程序执行期间根据需要添加或移除元素。以下是几种常见的实现和使用动态数组的方法。 ### 使用 `List<T>` 实现动态数组 `List<T>` 是 .NET Framework 提供的一个泛型集合类,它本质上是一个动态数组,能够自动扩展容量[^2]。以下是一个简单的示例: ```csharp List<string> dynamicList = new List<string>(); dynamicList.Add("hello"); dynamicList.Add("world"); dynamicList.Add("!"); // 输出当前列表中的元素数量 Console.WriteLine(dynamicList.Count); // 输出3 // 访问特定索引处的元素 Console.WriteLine(dynamicList[1]); // 输出"world" // 移除指定索引位置的元素 dynamicList.RemoveAt(1); // 再次输出元素数量和特定索引处的元素 Console.WriteLine(dynamicList.Count); // 输出2 Console.WriteLine(dynamicList[1]); // 输出"!" ``` ### 自定义动态数组类 如果希望更深入理解动态数组的工作原理,可以自定义一个动态数组类。这个类通常包含内部数组、当前元素计数以及用于管理数组大小的方法。下面是一个基本的实现示例: ```csharp public class DynamicArray<T> { private T[] array; private int count; public DynamicArray(int initialCapacity = 4) { array = new T[initialCapacity]; count = 0; } public void Add(T item) { if (count == array.Length) { ResizeArray(); } array[count++] = item; } public void RemoveAt(int index) { if (index < 0 || index >= count) { throw new ArgumentOutOfRangeException(nameof(index)); } for (int i = index; i < count - 1; i++) { array[i] = array[i + 1]; } array[--count] = default(T); } public T this[int index] { get { if (index < 0 || index >= count) { throw new ArgumentOutOfRangeException(nameof(index)); } return array[index]; } set { if (index < 0 || index >= count) { throw new ArgumentOutOfRangeException(nameof(index)); } array[index] = value; } } public int Count => count; private void ResizeArray() { T[] newArray = new T[array.Length * 2]; Array.Copy(array, newArray, count); array = newArray; } } ``` 通过上述代码创建的 `DynamicArray<T>` 类提供了添加、删除和访问元素的功能,并且当内部数组的空间不足时会自动扩容[^1]。 ### 使用场景 - **数据收集**:当你不确定需要存储多少数据时,例如从文件或网络读取未知数量的数据项。 - **算法实现**:某些算法(如排序或搜索)可能需要临时存储变化大小的数据集。 - **用户界面更新**:UI组件绑定到动态变化的数据源上,比如列表框或组合框的内容随用户操作而改变。 这些方法展示了如何在 C# 中有效地使用和实现动态数组,以适应不同的编程需求。无论选择内置的 `List<T>` 还是自定义实现,都可以灵活地处理动态数据集合的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值