Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Reversing Arrays | Array Manipulation and Sorting
/
C# Arrays

bookReversing Arrays

メニューを表示するにはスワイプしてください

Reversing an array means changing the order of its elements so that the first element becomes the last, the second becomes the second-to-last, and so on. This operation is useful in situations like undoing actions, displaying recent items first, or simply when you need to process data in the opposite order. Understanding how to reverse arrays will help you manipulate data more flexibly in your C# programs.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] numbers = { 1, 2, 3, 4, 5 }; int n = numbers.Length; // Reverse the array in place using a for loop for (int i = 0; i < n / 2; i++) { int temp = numbers[i]; numbers[i] = numbers[n - 1 - i]; numbers[n - 1 - i] = temp; } Console.WriteLine("Reversed array:"); foreach (int num in numbers) { Console.Write(num + " "); } } } }

The code above demonstrates how to reverse an array in place using a for loop. The algorithm works by swapping the element at the start of the array with the element at the end, then moving inward and repeating this process until you reach the middle of the array. This way, each pair of elements is swapped only once, and the reversal is completed efficiently without needing extra space for another array.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930313233
using System; namespace ConsoleApp { public class Program { public static void Main(string[] args) { int[] original = { 1, 2, 3, 4, 5 }; int n = original.Length; int[] reversed = new int[n]; // Create a new reversed array for (int i = 0; i < n; i++) { reversed[i] = original[n - 1 - i]; } Console.WriteLine("Original array:"); foreach (int num in original) { Console.Write(num + " "); } Console.WriteLine("\nReversed array (new):"); foreach (int num in reversed) { Console.Write(num + " "); } } } }

This code sample demonstrates how to create a new reversed array from the original array without modifying the original. It copies each element from the original array to a new array in reverse order. This approach allows you to keep the original array unchanged while providing a reversed version that you can use separately.

1. What is the main difference between in-place reversal and creating a reversed copy?

2. How many swaps are needed to reverse an array of length n?

3. Why might you want to preserve the original array when reversing?

question mark

What is the main difference between in-place reversal and creating a reversed copy?

正しい答えを選んでください

question mark

How many swaps are needed to reverse an array of length n?

正しい答えを選んでください

question mark

Why might you want to preserve the original array when reversing?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 3.  1

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 3.  1
some-alt