Moving elements in array c#

后端 未结 4 1613
感情败类
感情败类 2021-01-12 07:59

I have this very simple array which I want to be able to move around some items in. Are there any built in tools in c# to do this? If not, du you have any suggestion in how

4条回答
  •  一个人的身影
    2021-01-12 08:30

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace SecondLargest
    {
        class ArraySorting
        {
            public  static void Main()
            {
             int[] arr={5,0,2,1,0,44,0,9,1,0,23};
    
             int[] arr2 = new int[arr.Length];
             int arr2Index = 0;
             foreach (int item in arr)
             {
               if(item==0)
               {
                   arr2[arr2Index] = item;
                   arr2Index++;
               }
             }
             foreach (int item in arr)
             {
                if(item!=0)
                {
                    arr2[arr2Index] = item;
                    arr2Index++;
                }
             }
    
             foreach (int item in arr2)
             {
                 Console.Write(item+" ");
             }
    
                Console.Read();
    
            }
        }
    }
    

提交回复
热议问题