Selecting alternate items of an array C#

2019-01-29 07:33发布

I have an array statsname as

apple
X
banana
Y
Kiwi
z

I need to put apple,banana and Kiwi in an array Fruits and X,Y and Z in an array called alphabets.

Any simple C# mechanism for it please ?

9条回答
时光不老,我们不散
2楼-- · 2019-01-29 08:13

If i have understood you question correctly what you want is very simple: You want put fruits in array of fruits and same for alphabets and they are appearing alternatively in array statsname so:

for(int i=0,j=0;i<statsname.length;i+2,j++)
    fruits[j]=statsname[i];

for(int i=1,j=0;i<statsname.length;i+2,j++)
    alphabets[j]=statsname[i];
查看更多
【Aperson】
3楼-- · 2019-01-29 08:14

using only Arrays:

 var array = new string[] { "apple", "X", "banana", "Y", "Kiwi", "z" };
 var fruit = new string[array.Length];
 var alphabet = new string[array.Length];
 for(var i = 0,j = 0; i < array.Length / 2; i++, j += 2)
 {
     fruit[i] = array[j];               
     alphabet[i] = array[j + 1];
 }
查看更多
狗以群分
4楼-- · 2019-01-29 08:14

Here is some working code, hopefully this will be helpfull to you:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReadFile
{
    class Program
    {
        static void ReadFile(string filePath, List<string> custumerNames, List<int> phoneNumbers)
        {
            string line = string.Empty;
            var fileStream = new StreamReader(filePath);
            bool isPhoneNumber = true;

            while ((line = fileStream.ReadLine()) != null)
            {
                if (isPhoneNumber)
                {
                    phoneNumbers.Add(Convert.ToInt32(line));
                    isPhoneNumber = false;
                }
                else
                {
                    custumerNames.Add(line);
                    isPhoneNumber = true;
                }
            }

            fileStream.Close();
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Started reading the file...");
            List<string> custumersNamesList = new List<string>();
            List<int> custumersPhonesNumbers = new List<int>();

            ReadFile("SampleFile.txt", custumersNamesList, custumersPhonesNumbers);

            //Assuming both the list's has same lenght.
            for (int i = 0; i < custumersNamesList.Count(); i++)
            {
                Console.WriteLine(string.Format("Custumer Name: {0} , Custumer Phone Number: {1}",
                    custumersNamesList[i], Convert.ToString(custumersPhonesNumbers[i])));
            }

            Console.ReadLine();
        }
    }
}
查看更多
爷、活的狠高调
5楼-- · 2019-01-29 08:15

Single LINQ:

List<string> list = new List<string>() { "apple", "X", "banana", "Y", "Kiwi", "z" };
var result = list.Select((l, i) => new { l, i })
                 .GroupBy(p => p.i % 2)
                 .Select(x => x.Select(v => v.l).ToList())
                 .ToList();

Then you have a list of lists:

Linq result

查看更多
聊天终结者
6楼-- · 2019-01-29 08:22

Stolen from How to get Alternate elements using Enumerable in C#

var fruits = myArray.Where((t, i) => i % 2 == 0).ToArray();
var alphabets = myArray.Where((t, i) => i % 2 == 1).ToArray();
查看更多
何必那么认真
7楼-- · 2019-01-29 08:25
string[] rawarray = new string [] {"Apple","X" .....};
string[] Fruites = new string[rawarray.Length/2+1];
string[] Alphabets = new string[rawarray.Length/2];

For(int i=0; i<rawarray.Length;i++)
{
   if(i%2==0)
   {
     Fruits[i/2]=rawarray[i]; 
   }
   else
   {
     Alphabets[i/2]=rawarray[i]; 
   }   
}
查看更多
登录 后发表回答