- 1
Can I add elements to array by Console.ReadLine??
About Arrays
5 Answers
+ 3
//declare array here
//add elements here:
for(int i = 0; i < arr.Length; i++)
array[i] = Console.ReadLine();
+ 1
yes.
pass the elements in a variable and then pass that variable into the array.do this in a for loop
+ 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[ ] a = new int[10];
for (int k = 0; k < 10; k++) {
Console.WriteLine("enter the {0} element",k);
a[k]=Convert.ToInt32(Console.ReadLine());
}
for (int k = 0; k < 10; k++) {
Console.WriteLine(a[k]);
}
}
}
}
0
Thank u i got it
0
keep in mind you can only add a predetermined amount of items - for i < array.length; if you wanted to maintain adding things, that's where List comes in handy.