Reputation: 61
int L = 2;
int M = 0;
for (int i = 1; i <= 6; i++)
{
foreach (char c in ListLines[L])
{
if(c == 'A')arrayOne[M]++;
if(c == 'B')arrayTwo[M]++;
if(c == 'C')arrayThree[M]++;
}
L =+ 2;
M++;
}
Hi!
I'm learning C# at the moment, and I'm trying to create a for loop for my arrays.
All I need to know really is can I create an integer (int M
) and use that to define the object in the array? For example, arrayOne[M]
?
As this will allow me to create a counter for it that will let me create a loop.
Upvotes: 0
Views: 123
Reputation: 159
Yes you can use indexing on your arrays without problem, as for why you always get the same output, we would need to see the declarations of your arrays.
But in your case, it will make more sense to use a Dictionary instead of multiple arrays. See: http://csharp.net-informations.com/collection/dictionary.htm
As requested below, here is an example with a Dictionary:
var charDict = new Dictionary<char, int>
for (int i = 1; i <= 6; i++){
foreach (char c in ListLines[i]) {
charDict[c]++;
}
}
Note: This does not behave the same way as your code, since I honestly didn't get your code logic.
Upvotes: 4