我如何从统一数组中删除索引
我得到了此代码:
string[] changedHobbyList = listOfHobbies;
string[] finishedHobbyList = new string[3];
for (int i = 0; i < 3; i++)
{
int num = Random.Range(0, changedHobbyList.Length-1);
finishedHobbyList[i] = changedHobbyList[num];
for (int l = num -1 ; l < changedHobbyList.Length; l++)
{
changedHobbyList[l] = changedHobbyList[l+1];
}
}
来自 https://www.includehelp.com/dot-net/delete-an-element-from-given-position-position-from-aray-aray-aray-ray-using-c-sharp-program.aspx 我从范围错误中获得了索引,我知道我是如何获得的,但是我无法找到一种解决方法,以及使其正常工作,所以如果您有一个更好的主意,请,谢谢您
I got this code:
string[] changedHobbyList = listOfHobbies;
string[] finishedHobbyList = new string[3];
for (int i = 0; i < 3; i++)
{
int num = Random.Range(0, changedHobbyList.Length-1);
finishedHobbyList[i] = changedHobbyList[num];
for (int l = num -1 ; l < changedHobbyList.Length; l++)
{
changedHobbyList[l] = changedHobbyList[l+1];
}
}
from https://www.includehelp.com/dot-net/delete-an-element-from-given-position-from-array-using-c-sharp-program.aspx and I am getting an index out of bounds error and I know how im getting it, but I cant figure out a way to fix it, as well as just getting it working, so if you have a better idea, please and thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
num是什么值?您的两个阵列的长度是多少?
您的循环看起来像是将所有内容从右移到左移动。您可能会脱离界限,因为您的listofhobbies长度大于更换的HobbyBies,而changobbybies可能会发生在theinghobbylist [l+1]。
编辑:
审查了修订后的循环后,这是因为您在for循环中的检查时
l < changedHobbyList.Length
,当L比长度少1时,您会遇到界限,并且您访问数组,例如:
intArray[l+1];
考虑将其更改为:
for (int l = num -1 ; l < changedHobbyList.Length - 1; l++)
{
changedHobbyList[l] = changedHobbyList[l+1];
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
这基本上是创建一个新列表,然后用您输入的名称替换
this is basically creating a new list and then replacing it with the name of the one you inputed