我需要将一个大小不确定的数组在中点拆分为两个单独的数组。
该数组是使用 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);
}
}
}
您可以使用以下方法将数组拆分为 2 个单独的数组
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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)