Home > Back-end >  How to keep count of elements in arraylist during for loop
How to keep count of elements in arraylist during for loop

Time:12-15

using System;
using System.Collections;
using System.Collections.Generic;

namespace UseAllInArrasys
{

    class Program
    {

        public static void Main(string[] args) 
        {
            int num = 0;
            Console.WriteLine("=========Print numbers stored in ArrayList==========");

            ArrayList numbers = new ArrayList();


            for (int i = 0; i <= 5; i  )
            {
                Console.Write("Enter any number: ");
                while (!int.TryParse(Console.ReadLine(), out num))
                {
                    Console.Clear();
                    Console.Write("Invalid input");
                    Console.ReadLine();
                    Console.Clear();
                    Console.Write("Enter any number: ");

                }
                numbers.Add(num);
            }

            foreach (var item in numbers)
            {
                Console.WriteLine(item);
            }


        }

    }
}

I tried to get the below output Say for example When the code runs it execute and ask to Console.Write("Enter any number: "); If the user input blank or character then it jumps to Console.Write("Invalid input"); Again it asks for Console.Write("Enter any number: "); As soon as user input is digits code moves to next index in arraylist

But it should print the first digit entered after clearing the screen same for second and so on

In the same code how can I control for loop, instead of i<= 5 how can I mention the arraylist count or size of likewise so that loop executes for that many times only.

I tried with ArrayList numbers = new ArrayList(5); for (int i = 0; i <= numbers.Count; i ) //Capacity

but it does not work

CodePudding user response:

Just don't use ArrayList

Important

We don't recommend that you use the ArrayList class for new development. Instead, we recommend that you use the generic List<T> class. The ArrayList class is designed to hold heterogeneous collections of objects. However, it does not always offer the best performance. Instead, we recommend the following:

  • For a heterogeneous collection of objects, use the List<Object> (in C#) or List(Of Object) (in Visual Basic) type.
  • For a homogeneous collection of objects, use the List<T> class. See Performance Considerations in the List<T> reference topic for a discussion of the relative performance of these classes. See Non-generic collections shouldn't be used on GitHub for general information on the use of generic instead of non-generic collection types.

CodePudding user response:

It'll loop through based on the array count.

 for (int i = 0; i <= 5; i  )
        {
            Console.Write("Enter any number: ");
            while (!int.TryParse(Console.ReadLine(), out num))
            {
                Console.Clear();
                Console.Write("Invalid input");
                Console.ReadLine();
                Console.Clear();
                Console.Write("Enter any number: ");

            }
            numbers.Add(num);
             foreach (var item in numbers)
             {
               Console.WriteLine(item);
             }
        }

CodePudding user response:

Perhaps get the total count from the user and use it as the counter.

namespace UseAllInArrasys
{
    class Program
    {

        public static void Main(string[] args) 
        {
            var num = 0;
            Console.WriteLine("=========Print numbers stored in ArrayList==========");

            Console.Write("Enter how many numbers you want: ");
            int.TryParse(Console.ReadLine(), out var totalCount);
            IList integerList = Enumerable.Range(0, totalCount).ToList();;
            var fixedSize = ArrayList.FixedSize(integerList);


            for (var i = 0; i < totalCount; i  )
            {
                Console.Write("Enter any number: ");
                while (!int.TryParse(Console.ReadLine(), out num))
                {
                    Console.Clear();
                    Console.Write("Invalid input");
                    Console.ReadLine();
                    Console.Clear();
                    Console.Write("Enter any number: ");
                }
                Console.WriteLine($"You entered {num}");
                numbers.Add(num);
            }

            foreach (var item in fixedSize)
            {
                Console.WriteLine(item);
            }
        }
    } 
}

CodePudding user response:

Use numbers.Capacity instead of Count, here's the modified code:

    static void Main(string[] args)
    {
            int num = 0;
            Console.WriteLine("=========Print numbers stored in ArrayList==========");

            ArrayList numbers = new ArrayList(5);


            for (int i = 0; i < numbers.Capacity; i  )
            {
                Console.Write("Enter any number: ");
                while (!int.TryParse(Console.ReadLine(), out num))
                {
                    Console.Clear();
                    Console.Write("Invalid input");
                    Console.ReadLine();
                    Console.Clear();
                    Console.Write("Enter any number: ");

                }
                numbers.Add(num);
            }

            foreach (var item in numbers)
            {
                Console.WriteLine(item);
            }


        }

And as per the additional question you can capture the userInput for for later use by changing the for loop like this:

            for (int i = 0; i < numbers.Capacity; i  )
            {
                Console.Write("Enter any number: ");
                string userInput = Console.ReadLine();
                
                while (!int.TryParse(userInput, out num))
                {
                    Console.Clear();
                    Console.Write("Invalid input: "   userInput);
                    Console.ReadLine();
                    Console.Clear();
                    Console.Write("Enter any number: ");

                    userInput = Console.ReadLine();
                }
                numbers.Add(num);
            }
  •  Tags:  
  • c#
  • Related