Csharp/CSharp Tutorial/Data Structure/Array Reverse
Содержание
- 1 Additional Array Methods
- 2 Reverse an array
- 3 Reverse an array in a range
- 4 Reverse an array using Array.Reverse
- 5 Use the Reverse() method to reverse the elements in charArray
- 6 Use the Reverse() method to reverse the elements in intArray
- 7 Use the Reverse() method to reverse the elements in stringArray
Additional Array Methods
<source lang="csharp">using System; class ProgrammingLanguages {
static void Main() { string[] languages = new string[]{"C#", "COBOL", "Java","C++", "Visual Basic", "Pascal", "Fortran", "Lisp", "J#"}; System.Array.Sort(languages); String searchString = "COBOL"; int index = System.Array.BinarySearch(languages, searchString); System.Console.WriteLine("The wave of the future, {0}, is at index {1}.", searchString, index); System.Console.WriteLine("{0,-20}\t{1,-20}", languages[0], languages[languages.Length - 1]); System.Array.Reverse(languages); System.Console.WriteLine("{0,-20}\t{1,-20}", languages[0], languages[languages.Length - 1]); System.Array.Clear(languages, 0, languages.Length); System.Console.WriteLine("{0,-20}\t{1,-20}", languages[0], languages[languages.Length - 1]); System.Console.WriteLine("After clearing, the array size is: {0}", languages.Length); }
}</source>
Reverse an array
<source lang="csharp">using System;
class MainClass {
public static void Main() { int i,j; int[] nums1 = new int[10]; int[] nums2 = new int[10]; for(i=0; i < nums1.Length; i++) nums1[i] = i*i; Console.Write("Original contents: "); for(i=0; i < nums2.Length; i++) Console.Write(nums1[i] + " "); Console.WriteLine(); Console.Write("reverse copy nums1 to nums2"); if(nums2.Length >= nums1.Length) // make sure nums2 is long enough for(i=0, j=nums1.Length-1; i < nums1.Length; i++, j--) nums2[j] = nums1[i]; Console.Write("Reversed contents: "); for(i=0; i < nums2.Length; i++) Console.Write(nums2[i] + " "); Console.WriteLine(); }
}</source>
Original contents: 0 1 4 9 16 25 36 49 64 81 reverse copy nums1 to nums2Reversed contents: 81 64 49 36 25 16 9 4 1 0
Reverse an array in a range
<source lang="csharp">using System;
class MainClass {
public static void Main() { int[] nums = { 1, 2, 3, 4, 5 }; Console.Write("Original order: "); foreach(int i in nums) Console.Write(i + " "); Console.WriteLine(); // Reverse a range. Array.Reverse(nums, 1, 3); // Display reversed order. Console.Write("Range reversed: "); foreach(int i in nums) Console.Write(i + " "); Console.WriteLine(); }
}</source>
Original order: 1 2 3 4 5 Range reversed: 1 4 3 2 5
Reverse an array using Array.Reverse
<source lang="csharp">using System;
class MainClass {
public static void Main() { int[] nums = { 1, 2, 3, 4, 5 }; Console.Write("Original order: "); foreach(int i in nums) Console.Write(i + " "); Console.WriteLine(); Array.Reverse(nums); Console.Write("Reversed order: "); foreach(int i in nums) Console.Write(i + " "); Console.WriteLine(); }
}</source>
Original order: 1 2 3 4 5 Reversed order: 5 4 3 2 1
Use the Reverse() method to reverse the elements in charArray
<source lang="csharp">using System; class MainClass {
public static void Main() { char[] charArray = {"w", "e", "l", "c", "o", "m", "e"}; Array.Sort(charArray); // sort the elements Array.Reverse(charArray); Console.WriteLine("Reversed charArray:"); for (int i = 0; i < charArray.Length; i++) { Console.WriteLine("charArray[" + i + "] = " + charArray[i]); } }
}</source>
Reversed charArray: charArray[0] = w charArray[1] = o charArray[2] = m charArray[3] = l charArray[4] = e charArray[5] = e charArray[6] = c
Use the Reverse() method to reverse the elements in intArray
<source lang="csharp">using System; class MainClass {
public static void Main() { int[] intArray = {5, 2, 3, 1, 6, 9, 7, 14, 25}; Array.Sort(intArray); Array.Reverse(intArray); Console.WriteLine("Reversed intArray:"); for (int i = 0; i < intArray.Length; i++) { Console.WriteLine("intArray[" + i + "] = " + intArray[i]); } }
}</source>
Reversed intArray: intArray[0] = 25 intArray[1] = 14 intArray[2] = 9 intArray[3] = 7 intArray[4] = 6 intArray[5] = 5 intArray[6] = 3 intArray[7] = 2 intArray[8] = 1
Use the Reverse() method to reverse the elements in stringArray
<source lang="csharp">using System; class MainClass {
public static void Main() { string[] stringArray = {"t", "i", "a", "test", "abc123", "abc345"}; Array.Sort(stringArray); Array.Reverse(stringArray); Console.WriteLine("Reversed stringArray:"); for (int i = 0; i < stringArray.Length; i++) { Console.WriteLine("stringArray[" + i + "] = " + stringArray[i]); } }
}</source>
Reversed stringArray: stringArray[0] = test stringArray[1] = t stringArray[2] = i stringArray[3] = abc345 stringArray[4] = abc123 stringArray[5] = a