Made filling the collection with random elements. The question is how to add letters to the random? And set the length of the element?

static void Main(string[] args) { Console.Write("Введит количество элементов коллекции: "); int i = Int32.Parse(Console.ReadLine()); ArrayList list = NewCollection(i); WriteMyCollection(list); Console.ReadLine(); } public static ArrayList NewCollection(int i) { Random rand = new Random(); ArrayList arr = new ArrayList(); for (int j = 0; j < i; j++) arr.Add(rand.Next()); return arr; } public static void WriteMyCollection(ArrayList arr) { foreach (int a in arr) Console.Write("{0}\t", a); Console.WriteLine("\n"); } 
  • And what kind of items do you want in the collection? Characters? Strings? How long is the string, to what extent? (And why an untyped collection, 2016 is the same in the yard!) - VladD
  • elements must be strings. the length of the element must be specified by the user (as well as the number of elements). This is an educational task for testing the performance of collections, so the choice seems strange)) - cruim

1 answer 1

Well, like this:

 static void Main(string[] args) { ArrayList al = new ArrayList(); var collectionLength = 20; for (int i = 0; i < collectionLength; i++) al.Add(CreateRandomString()); foreach (var s in al) Console.WriteLine(s); } static Random r = new Random(); static int maxStringLength = 10; static string chars = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; static string CreateRandomString() { var length = r.Next(maxStringLength) + 1; var sb = new StringBuilder(length); for (int i = 0; i < length; i++) { var randomIndex = r.Next(chars.Length); sb.Append(chars[randomIndex]); } return sb.ToString(); } 

I brought:

1JE
I0v7e4Tz4
AYq7QLXc
a17EEP
25Kke
A5cNhWu5D6
tbduDUIhO0
QAvQh
2OOJsEnkT
t
H
wVc0r
Vn012q
7eChqEG2
g00xP9JQe
6FW
nknHSDDMG2
g6gL2l1rG
30UKMEj
Lj60jeX2