Create a two-column list using foreach

I have a list initially consisting of 1 line:

One Two Three Four Five Six Seven 

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

  One Two Three Four Five Six Seven 

I am trying to do 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 } } 
+4
source share
9 answers

I would suggest LINQ:

 var odds = mod.Where((item, index) => index % 2 == 0).ToList(); var evens = mod.Where((item, index) => index % 2 == 1).ToList(); 
+2
source

Every time you update i . Move your ad 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); } } 
+2
source

The 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]); } 
+2
source

What 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++; } 
+1
source

Based on your last question, I assume that you want to do this:

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

You can create two lists and save 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
source

Here is the corrected version of your algorithm, which captures the fact that you will always compare the value i = 1, because your int declaration I was in a 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 } } 
+1
source

A simple bool will be enough.

 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; } 
+1
source

If you need a list with two columns, you can 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)); 
+1
source

Source: https://habr.com/ru/post/1391720/


All Articles