Click here to Skip to main content
15,662,484 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am new to .NET. I want to reverse an array i have done sample program but i am getting error.

C#
class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[6] { 1, 2, 3, 4, 5, 6, };
            //int[] b = new int[6];
            //int[] c = new int[6];
            Console.WriteLine("Elements in Array are:-");
            for (int i = a.Length; i >= 0; i--)
            {
                Console.WriteLine(a[i] + "");
            }
            Console.ReadLine();
        }


    }
}


i am getting error like this.
Index was outside the bounds of the array

thank you.
Posted

You're doing it the hard way.

You only need a single line of code:
class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[6] { 1, 2, 3, 4, 5, 6, };

            Array.Reverse(a);

            Console.WriteLine("Elements in Array are:");
            for (int i = 0; i < a.Length; i++)
            {
                Console.WriteLine(a[i] + "");
            }
            Console.ReadLine();
        }
    }
}

Keep in mind, the Length of any array will tell you how many elements there are in it. For for an array of Length 10, the elements will be indexed 0 through 9. That's why your code was throwing an "Index out of bounds" error.
 
Share this answer
 
Comments
Shahin Khorshidnia 4-Mar-12 15:05pm    
+4 (not 5) because you retouched the array. You could make an alternative array for reversing it.
Hello

C#
static void Main(string[] args)
     {
         int[] a = new int[6] { 1, 2, 3, 4, 5, 6, };

         Console.WriteLine("Elements in Array are:-");
         for (int i = a.Length; i > 0; i--)
         {
             Console.WriteLine(a[i - 1] + " ");
         }
         Console.ReadLine();
     }


Or

C#
static void Main(string[] args)
{
    int[] a = new int[6] { 1, 2, 3, 4, 5, 6, };

    Console.WriteLine("Elements in Array are:-");
    for (int i = a.Length - 1; i >= 0; i--)
    {
        Console.WriteLine(a[i] + "");
    }
    Console.ReadLine();
}


Or

C#
int[] a = new int[6] { 1, 2, 3, 4, 5, 6, };

int[] revers = a.Reverse().ToArray();

Console.WriteLine("Elements in Array are:-");
foreach (int i in revers)
{
    Console.WriteLine(i + "");
}
 
Share this answer
 
v3
Comments
Dave Kreskowiak 4-Mar-12 13:50pm    
Uhhh... a.Reverse isn't going to work and why would you call ToArray on an array?

It should be:

Array.Reverse(a);
Shahin Khorshidnia 4-Mar-12 15:00pm    
And that's why you voted 1 (not 4, 3, even 2) to an accepted solution? and ofcourse it's working.


Anyway Thanks for telling the reason.
Dave Kreskowiak 4-Mar-12 22:51pm    
I didn't vote you a 1, or anything else for that matter.
Shahin Khorshidnia 5-Mar-12 2:20am    
Ops! Sorry about the groundless accusation :D

But somebody is downvoting my comments (without any reason) and I don't know who and why?!

Pardon me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900