Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cycling through contents of array issue

Tags:

c#

I am using an int array to hold a long list of integers. For each element of this array I want to check if it is 1, if so do stuff relevant to 1 only, else if it is a 2, do other stuff relevant to 2, and so forth for each value stored in the array. I came up with the code below but it isn't working as expected, is there something I am missing? What is happening is that only the first value of the array is being considered.

int[] variable1 = MyClass1.ArrayWorkings();
foreach (int i in variable1)
{ 
    if (variable1[i] == 1)
    {
        // arbitrary stuff
    }
    else if (variable1[i] ==2)
    {
        //arbitrary stuff
    }
}
like image 932
user1166981 Avatar asked Jun 26 '12 06:06

user1166981


3 Answers

The i in your foreach loop holds the actual element value from the array in each iteration, not the index. In your particular code sample, your array probably holds only zeroes, which is why you're only getting the first element (you're always using index 0). Hence, you should check i rather than variable1[i].

If you are going to check against various integer constants, a switch expression is more suitable, BTW:

foreach (int i in variable1) {
    switch (i) {
        case 1:
            // arbitrary stuff
            break;
        case 2:
            // arbitrary stuff
            break;
    }
}

switch/case saves you some writing; if you ever pull your values from anywhere else than from i, you can simply change the (i) part in the switch statement, and moreover, switch might be evaluated by the compiler more efficiently than the chained if-else statements.

Note: You will not be able to directly change array values in the foreach loop, as you cannot assign anything to i. If you need to assign new array values, you'll have to

  • count yourself with an additional variable while still using foreach or
  • use another loop such as for and retrieve the item at the current index yourself.
like image 86
O. R. Mapper Avatar answered Oct 17 '22 22:10

O. R. Mapper


You're trying to do something that doesn't make sense. To see how it works, take a simple example, an array with values: 9, 4, 1.

If you try to run your code on this sample array, you'll get an error:

foreach (int i in variable1)
{ 
    if (variable1[i] == 1)   // the item i is 9.  
                             // But variable[i] means, get the value at position #9 in the array
                             // Since there are only 3 items in the array, you get an Out Of Range Exception
    {
        // arbitrary stuff
    }
{

Instead, this is what you need:

foreach (int i in variable1)  // i will be 9, then 4, then 1)
{ 
    if (i == 1)   
    {
        // arbitrary stuff
    }
    // ... etc
}

The alternative is to use a for loop, which would give you the indices 0, 1, and 2, like this:

for (int i=0 ; i<=variable1.Length ; i++)   // i will be 0, 1, 2
                                            // variable[i] will be 9, 4, 1
{
    if (variable1[i] == 1) 
    { 
        // stuff
    }

    // ... etc
}
like image 4
McGarnagle Avatar answered Oct 17 '22 23:10

McGarnagle


Write like this instead:

foreach (int i in variable1) {
    if (i == 1) {
    ....
like image 2
Asken Avatar answered Oct 17 '22 23:10

Asken