Repeat method in ArrayList

In this article I will discuss the Repeat method of ArrayList.
  • 4579

Introduction

Repeat method returns an ArrayList whose elements are copies of the specified value. That means in this method we can give a specified value and then this value will be copied to the arraylist. The number of times that this value is printed is the value that we give as an argument to the repeat method of arraylist.

Example

namespace ConsoleApplication3

{

    class Program

    {

        static void Main(string[] args)

        {

            // Creates a new ArrayList with five elements and

            //initialize each element with a null value.

            ArrayList myArray = ArrayList.Repeat(null, 10);

            // Displays the count, capacity and values of the ArrayList.

            Console.WriteLine("ArrayList with five elements with a null value");

            Console.WriteLine("   Count    : {0}", myArray.Count);

            Console.WriteLine("   Capacity : {0}", myArray.Capacity);

            Console.Write("   Values:");

            foreach (Object obj in myArray)

                Console.Write("   {0}", obj);

            Console.WriteLine();

            // Creates a new ArrayList with seven elements and

            //initialize each element with the string "Repeat".

            myArray = ArrayList.Repeat("Repeat", 5);

            // Displays the count, capacity and values of the ArrayList.

            Console.WriteLine("ArrayList with seven elements with a string value");

            Console.WriteLine("   Count    : {0}", myArray.Count);

            Console.WriteLine("   Capacity : {0}", myArray.Capacity);

            Console.Write("   Values:");

            foreach (Object obj in myArray)

                Console.Write("   {0}", obj);

            Console.WriteLine();

        }

    }

}


Output

repeat.jpg

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.