4

I have a list that is just 1 row initially as such:

    One
    Two
    Three
    Four
    Five 
    Six
    Seven

I would then have the following in a list - note how I have 2 columns - first column is for odd number, second column is for even number:

    One     Two
    Three   Four
    Five    Six
    Seven

I am trying the following:

foreach(var item in mod)
{
    int i = 0;

    i = i + 1;
    if (i % 2 == 0) 
    {                  
        //add to list here for even number 
    }

    if (i % 2 != 0)
    {
        // add to list here for odd number
    } 
}  
2
  • 1
    You can replace the 2nd if statement with an else statement. Commented Jan 18, 2012 at 22:40
  • 2
    I don't think you meant to declare i inside the loop. Commented Jan 18, 2012 at 22:41

9 Answers 9

2

I'd suggest LINQ:

var odds = mod.Where((item, index) => index % 2 == 0).ToList();
var evens = mod.Where((item, index) => index % 2 == 1).ToList(); 
Sign up to request clarification or add additional context in comments.

3 Comments

But this is two passes through the input list rather one. While not a big thing for this small data set it would have an impact on performance if the input list were large.
Aren't you iterating through the collection twice with this?
True, but until we know that the size is large enough to warrant it, I prefer code simplicity over speed.
2

You're redeclaring i each time. Move the declaration outside of the foreach.

List<int> even = new List<int>();
List<int> odd = new List<int>();

int i = 0;
foreach (var item in mod)
{
    i = i + 1;
    if (i % 2 == 0)
    {
        even.Add(i);
    }
    else
    {
        odd.Add(i);
    }
}

Comments

2

Previous answer had an error:

static void Main(string[] args)
    {
        var mod = new string[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven" };

        var OddNumbers = new List<string>();
        var EvenNumbers = new List<string>();
        int i = 0;
        foreach (var item in mod)
        {

            i = i + 1;
            if (i % 2 == 0)
            {
                EvenNumbers.Add(item);
            }
            else
            {
                OddNumbers.Add(item);
            }
        }
}

        // but when you use an index in your loop I find it more readable to use this
        for (var j = 0; j < mod.Length; ++j)
        {
            if (j % 2 == 0)
                 OddNumbers.Add(mod[j]);
            else
                EvenNumbers.Add(mod[j]);
        }

Comments

1

How about:

int i = 0;
foreach(var item in mod)  //I assume 'mod' is a collection of ints?
{             
    if (i % 2 == 0)
    {
        //Do something with 'item'.
    }
    else 
    {
        //Do something else with 'item'.
    }

    i++;
}

Comments

1

Based on your last question, I'm guessing you want to do this:

int i = 0; 
foreach(var item in mod) 
{ 
     ..
     // rest of foreach loop here
}

Comments

1

You can create two lists and store the numbers depending on their type:

List<int> OddNumbers = new List<int>();
List<int> EvenNumbers = new List<int>();

then do the following:

         foreach(var item in mod) 
         { 
             if (item % 2 == 0)  
             {                   
                EvenNumbers.Add(item);
             } 
             else 
             {
                OddNumbers.Add(item);
             }
         } 

1 Comment

Pretty sure that all the numbers are going to be odd in your code sample.
1

Here is a corrected version of your algoryth that fixes the fact that you'll alway be comparing a value of i = 1 because your declaration of int i was in the loop

    int i = 0; 
     foreach(var item in mod)
     {
         i++;
         if (i % 2 == 0) 
         {                  
            //add to list here for even number 
         }
         else
         {
          // add to list here for odd number
         } 
     }  

Comments

1

A simple bool will suffice.

var odds = new List<string>();
var evens = new List<string>();

bool odd = true;
foreach (var item in new[] {"one", "two", "three", "four"})
{
    if (odd)
        odds.Add(item);
    else
        evens.Add(item);

    odd = !odd;
}

Comments

1

If you want a list with two columns you could use tuples like this.

            List<Tuple<List<int>, List<int>>> listTest = new List<Tuple<List<int>, List<int>>>();

            List<int> evenNumber = new List<int>();
            List<int> oddNumber = new List<int>();

            int i = 0;

            foreach (var item in mod)
            {

                if (i % 2 == 0)
                {
                   //add to list here for even number 
                    evenNumber.Add(i);                      
                }

                if (i % 2 != 0)
                {
                    // add to list here for odd number
                    oddNumber.Add(i);  
                }

                i++;
            }
            listTest.Add(Tuple.Create(oddNumber, evenNumber));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.