我如何从统一数组中删除索引

发布于 01-21 14:06 字数 861 浏览 5 评论 0原文

我得到了此代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心凉2025-01-28 14:06:56
    string[] changedList = new string[] {"hi,"cheese","more cheese"};
    string[] changingList = new string[changedList.Length-1];

    int num = *index being removed*;

    int spot = 0;
        
    for (int l = 0; l < changedList.Length; l++)
    {
        if (l != num){
            changingList[spot] = changedList[l];
            spot++;
        changedList = changingList;
    }

这基本上是创建一个新列表,然后用您输入的名称替换

    string[] changedList = new string[] {"hi,"cheese","more cheese"};
    string[] changingList = new string[changedList.Length-1];

    int num = *index being removed*;

    int spot = 0;
        
    for (int l = 0; l < changedList.Length; l++)
    {
        if (l != num){
            changingList[spot] = changedList[l];
            spot++;
        changedList = changingList;
    }

this is basically creating a new list and then replacing it with the name of the one you inputed

埋情葬爱2025-01-28 14:06:56

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];
    }

What value is num? What is the lengths of your two arrays?

Your for loop looks like it's shifting everything from right to left by one. It's possible you're going out of bounds because your listOfHobbies length is greater than changedHobbies which could happen at changedHobbyList[l+1].

EDIT:

After reviewing the revised loop, it's because of your check in the for loop

l < changedHobbyList.Length

You're going out of bounds when l is 1 less than the length and you access the array like:

intArray[l+1];

consider changing it to:

    for (int l = num -1 ; l < changedHobbyList.Length - 1; l++)
    {
        changedHobbyList[l] = changedHobbyList[l+1];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文