Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Splitting An Array

Tags:

arrays

c#

I need to split an array of indeterminate size, at the midpoint, into two separate arrays.

The array is generated from a list of strings using ToArray().

        public void AddToList ()         {             bool loop = true;             string a = "";              Console.WriteLine("Enter a string value and press enter to add it to the list");             while (loop == true)             {                 a = Console.ReadLine();                  if (a != "")                 {                     mylist.Add(a);                 }                 else                 {                     loop = false;                 }             }          }          public void ReturnList()         {             string x = "";             foreach (string number in mylist)             {                 x = x + number + " ";             }             Console.WriteLine(x);             Console.ReadLine();         }      }      class SplitList     {         public string[] sTop;         public string[] sBottom;          public void Split(ref UList list)           {             string[] s = list.mylist.ToArray();              //split the array into top and bottom halfs          }     }      static void Main(string[] args)     {         UList list = new UList();         SplitList split = new SplitList();          list.AddToList();         list.ReturnList();          split.Split(ref list);     } } 

}

like image 995
TheValheruGod Avatar asked Dec 03 '09 16:12

TheValheruGod


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C hard to learn?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

You could use the following method to split an array into 2 separate arrays

public void Split<T>(T[] array, int index, out T[] first, out T[] second) {   first = array.Take(index).ToArray();   second = array.Skip(index).ToArray(); }  public void SplitMidPoint<T>(T[] array, out T[] first, out T[] second) {   Split(array, array.Length / 2, out first, out second); } 
like image 128
JaredPar Avatar answered Sep 30 '22 18:09

JaredPar


Use a generic split method:

public static void Split<T>(T[] source, int index, out T[] first, out T[] last) {     int len2 = source.Length - index;     first = new T[index];     last = new T[len2];     Array.Copy(source, 0, first, 0, index);     Array.Copy(source, index, last, 0, len2); } 
like image 30
Cecil Has a Name Avatar answered Sep 30 '22 16:09

Cecil Has a Name