gpt4 book ai didi

C# 拆分数组

转载 作者:IT王子 更新时间:2023-10-29 04:31:56 29 4
gpt4 key购买 nike

我需要在中点将一个不确定大小的数组拆分为两个单独的数组。

数组是使用 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);
}

关于C# 拆分数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1841246/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com