Reverse elements in an array

后端 未结 6 1869
既然无缘
既然无缘 2020-11-30 14:58

I setup an array and i would like to create a method that will return an array with the elements in reverse. e.g. if there are 10 slots then array1[9] = 6 so

相关标签:
6条回答
  • 2020-11-30 15:42

    You should be able to use the extension method Reverse

    int[] arr = { 43, 22, 1, 44, 90, 38, 55, 32, 31, 9 };
    Console.WriteLine("Before");
    PrintArray(arr);
    Console.WriteLine("After");
    PrintArray(arr.Reverse().ToArray());
    

    Or if you don't mind modifing the original sequence you can use Array.Reverse

    int[] arr = { 43, 22, 1, 44, 90, 38, 55, 32, 31, 9 };
    Console.WriteLine("Before");
    PrintArray(arr);
    Array.Reverse(arr);
    Console.WriteLine("After");
    PrintArray(arr);
    
    0 讨论(0)
  • 2020-11-30 15:44

    You can reverse an array by swapping elements from both ends of the array till you reach the middle.

    for(int start = 0, end = arr.Length - 1; start < arr.Length/2; start++, end--)
    {
         swap(arr[start], arr[end]);
    }
    
    0 讨论(0)
  • 2020-11-30 15:47

    Create a new array that has the same length as the old array and reverse is done like this

    index -> new index  
      0   -> length - 1  
      1   -> length - 1 - 1  
      2   -> length - 1 - 2  
    ..  
      i   -> length - 1 - i
    

    so this translates to this code

    int[] myVariable = new int[] { 1, 2, 3, 4, 5 };
    int[] reversedVariable =  new int[myVariable.Length]();
    for (int i = 0; i < myVariable.Length ; i++)
    {
        reversedVariable[myVariable.Length - 1 - i] = myVariable[i];
    }
    return reversedVariable;
    
    0 讨论(0)
  • 2020-11-30 15:51
           int a;
            Console.WriteLine("Enter the array length");
            a=int.Parse(Console.ReadLine());
            Console.WriteLine("enter the array element");
            int[] sau = new int[a];
            for (int i = 0; i < a; i++)
            {
                sau[i] = int.Parse(Console.ReadLine());
            }
            for (int i =a.Length-1 ; i < 0;i--)
            {
                Console.WriteLine(sau[i]);
            }
                Console.ReadLine();
        }
    }
    

    }

    0 讨论(0)
  • 2020-11-30 16:02
     public class MyList<T> : IEnumerable<T>
    {
        private T[] arr;
    
        public int Count
        {
            get { return arr.Length; }
        }            
    public void Reverse()
        {
            T[] newArr = arr;
            arr = new T[Count];
            int number = Count - 1;
            for (int i = 0; i < Count; i++)
            {
                arr[i] = newArr[number];
                number--;
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 16:05

    You can use Array.Reverse

    Example from above line

    public static void Main()  {
    
      // Creates and initializes a new Array.
      Array myArray=Array.CreateInstance( typeof(String), 9 );
      myArray.SetValue( "The", 0 );
      myArray.SetValue( "quick", 1 );
      myArray.SetValue( "brown", 2 );
      myArray.SetValue( "fox", 3 );
      myArray.SetValue( "jumps", 4 );
      myArray.SetValue( "over", 5 );
      myArray.SetValue( "the", 6 );
      myArray.SetValue( "lazy", 7 );
      myArray.SetValue( "dog", 8 );
    
      // Displays the values of the Array.
      Console.WriteLine( "The Array initially contains the following values:" );
      PrintIndexAndValues( myArray );
    
      // Reverses the sort of the values of the Array.
      Array.Reverse( myArray );
    
      // Displays the values of the Array.
      Console.WriteLine( "After reversing:" );
      PrintIndexAndValues( myArray );
    }
    
    
    public static void PrintIndexAndValues( Array myArray )  {
      for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
         Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
    }
    
    0 讨论(0)
提交回复
热议问题