Display elements of ArrayList

In this article I will explain how to display the elements of ArrayList.
  • 2086

Introduction

In this article I will discuss how to display the elements of the arraylist. In order to achieve this we can use for loop or foreach loop. Mostly we can use foreach loop to display the elements of the arraylist.In this article I will explain the use of both for and foreach loop.

Example

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            //create the arraylist

            ArrayList arraylist1 = new ArrayList();

            arraylist1.Add(5);

            arraylist1.Add(7);

            arraylist1.Add(10);

            arraylist1.Add(15);

            arraylist1.Add(23);

            //display elements of the arraylist using foreach loop

            Console.WriteLine("The elements of the arraylist are:");

            foreach (int i in arraylist1)

            {

                Console.WriteLine(i);

            }

            //display elements of the arraylist using for loop

            Console.WriteLine("The elements of the arraylist are:");

            for (int i = 0; i <=4; i++)

            {

                Console.WriteLine(arraylist1[i]);

            }

        }

    }

}

 

Output

display_element.jpg 

Ask Your Question 

Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.