【问题标题】:Reverse elements in an array反转数组中的元素
【发布时间】:2013-08-23 04:48:26
【问题描述】:

我设置了一个数组,我想创建一个方法,该方法将返回一个包含反向元素的数组。例如如果有 10 个插槽,则为 array1[9] = 6,则为 array2[0] = 6
我想我必须返回一个数组 - 我该怎么做?
而且我不知道如何反转并添加到另一个数组。
谢谢!

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

         Console.ReadKey(true);

    }

    static int[] Reverse(int[] array)
    {
        for (int i = array.Length; i < 1; i--)
        {
            int x = 0;

            array[i] = array[x++];
            Console.WriteLine(array[i]);
        }

       }


         static void PrintArray(int[] array)
    {
        for (int j = 0; j < array.Length; j++)
        {
            Console.Write(array[j] + " ");

        }
        Console.WriteLine("");

【问题讨论】:

  • 你不能只使用`array.Reverse();`吗?

标签: c# arrays reverse


【解决方案1】:

您可以使用Array.Reverse

上面的例子

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 ) );
}

【讨论】:

    【解决方案2】:

    你应该可以使用扩展方法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());
    

    或者如果您不介意修改原始序列,您可以使用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);
    

    【讨论】:

    • 你一团糟:其实-arr.Reverse()没有改变原来的,而Array.Reverse(arr)-改变了!
    • 这就是我所说的。 "如果您不介意修改原始序列,您可以使用 Array.Reverse"
    • 对于想要在第一个示例中使用 Reverse 的任何人来说,仅供参考:您需要“使用 System.Linq”。
    【解决方案3】:

    您可以通过交换数组两端的元素直到到达中间来反转数组。

    for(int start = 0, end = arr.Length - 1; start < arr.Length/2; start++, end--)
    {
         swap(arr[start], arr[end]);
    }
    

    【讨论】:

    • arr.length -> arr.Length
    • 好的,谢谢!我是考虑 Java 写的,现在我意识到 OP 标记了 C#。
    【解决方案4】:
           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();
        }
    }
    

    }

    【讨论】:

      【解决方案5】:

      创建一个与旧数组长度相同的新数组,反向操作是这样完成的

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

      所以这会转化为这段代码

      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;
      

      【讨论】:

        【解决方案6】:
         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--;
                }
            }
        }
        

        【讨论】:

        • 请添加一些您的代码在做什么的解释。
        • 在 Reverse() 方法中,您可以找到一种方法来反转数组中的元素。这是 cod 的一部分,您可以使用它创建任何类型的集合并反转集合元素
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多