C#阵列Array排序

五一假期回来,练习一下C#的一些知识,了解一下排序。

练习数据:

int[] ints = { 16, 75, 1, 39, 22, 43, 3, 68, 55 };


写一个类:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationDemo
{
    public class Bw
    {
        public int[] ArrayData { get; set; }

        public Bw() { }

        public Bw(int[] myArrayData)
        {
            this.ArrayData = myArrayData;
        }      
    }
}
Source Code

 

为这个类,添加一个方法,arrayToArrayListWithForeach() 即是使用foreach方法,把array数据copy to ArrayList数据集:

 

 System.Collections.ArrayList _al = new System.Collections.ArrayList();

        public void arrayToArrayListWithForeach()
        {
            foreach (int i in ArrayData)
            {
                _al.Add(i);
            }
        }
Source Code

 

把Array数据copy to ArrayList,还可以使用另外的方法,arrayToArrayListWithAddRange()



 public void arrayToArrayListWithAddRange()
        {
            _al.AddRange(ArrayData);
        }
Source Code

 

为上面的类,写一个ArrayList数据集Sort();

 

public void Sort()
        {
            _al.Sort();
        }
Source Code

 

再为类写一个方法,就是输出ArrayList的数据:

 

 public void Output()
        {
            foreach (int i in _al)
            {
                Console.WriteLine(i.ToString());
            }
        }
Source Code

 

所需要的方法,均写完,在控制台程序使用它们了。

 

 

上面#17,#18行代码,可以在类new时,一起传入:

上面#20行代码,由于我们在Bw这个类别中,有写了另外一个方法,所以,也可以这样子:

OK,实现对数据进行排序:

 

转载于:https://www.cnblogs.com/insus/p/10825174.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# provides two main data structures for storing collections of elements: List and Array. Both List and Array are used to store and manipulate a collection of items, but they have some differences in terms of flexibility and functionality. An Array is a fixed-size collection of elements of the same type. Once an array is created, its size cannot be changed. You need to specify the size of the array when declaring it. Array elements can be accessed using an index, starting from 0. Here's an example of creating and accessing elements in an array: ```csharp int[] numbers = new int[5]; // Creating an array of size 5 numbers[0] = 1; // Accessing and setting the value at index 0 int firstNumber = numbers[0]; // Accessing the value at index 0 ``` On the other hand, List is a dynamic collection that can grow or shrink in size. It allows adding, removing, and modifying elements easily. List provides several methods and properties to manipulate the collection efficiently. Here's an example of creating and manipulating elements in a List: ```csharp List<int> numbers = new List<int>(); // Creating an empty list numbers.Add(1); // Adding an element to the list int firstNumber = numbers[0]; // Accessing the value at index 0 numbers.Remove(1); // Removing an element from the list ``` In summary, Arrays have a fixed size, while Lists can dynamically grow or shrink. Arrays are generally more efficient in terms of memory, but Lists provide more flexibility and functionality for manipulating collections.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值