I’m Coding !(一)

1、冒泡排序:

需要用两个for循环才能遍历所有的数组元素。2个for循环的用处,就是第一个for循环的每一个分别去比较后一个for循环的所有元素。

 public void addsort(int[] array)
        {
            int length = array.Length;
            for (int y = 0; y < length - 1; y++)
            {

            
                for (int i = 0; i < length - 1; i++)
                {
                    if (array[i] > array[i + 1])
                    {

                        int temp = array[i];
                        array[i] = array[i + 1];
                        array[i + 1] = temp;
                    }}}}
              
        static void Main(string[] args)
        {
            int[] array = { 1, 4, 2, 5, 3 };
            abc ssh = new abc();
            ssh.addsort(array);
            foreach (int i in array)
            {
                Console.WriteLine("{0}", i);
            }
            Console.WriteLine();

        }
    }
}

2、 简单的泛型方法:

class bcd
    {
        public static int Skyy<T>(T[] item, T items)    //创建泛型方法
        {
            for (int i = 0; i < item.Length;i++ )
            {
                if (item[i].Equals(items))
                {
                    return i;
                }
            }
            return -1;
        }
    }

------ 

class Class1
    {

 static void Main(String[] args)
        {
            int i = bcd.Skyy<int>(new int[] { 1, 2, 3, 4, 5 }, 3);
            Console.WriteLine("3在数组中的位置" + i.ToString());
        }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值