删除方法不删除

时间:2014-11-03 03:36:17

标签: c#

我正在创建一个包含三个阵列的程序:一个用于人的姓,一个用于得分,一个用于玩家编号。现在,我得到了所有阵列,但一切都已完成,但是由于某种原因,我的进程删除方法不会从数组中删除项目。我知道我问过类似的问题,但我似乎无法弄清楚为什么它不会被正确删除

任何帮助都将不胜感激,谢谢

static Int32[] ProcessDelete(Int32[] playerNumbers, ref Int32 playerCount, 
    String[] playerLastName, Int32[] playerPoints )
{
    Int32[] newArray = new Int32[playerNumbers.Length - 1]; 
    String[] newArray2 = new String[playerLastName.Length - 1]; 
    Int32[] newArray3 = new Int32[playerPoints.Length - 1];

    int index = 0;
    int index2 = 0;
    int index3 = 0;
    int j = 0;
    int k = 0;
    int t = 0;

    while (index < playerNumbers.Length)
    {
        if (index != playerCount)
        {
            newArray[j] = playerNumbers[index];
            j++;
        }

        index++;
    }

    while (index2 < playerLastName.Length)
    {
        if (index2 != playerCount)
        {
            newArray2[k] = playerLastName[index2];
            k++;
        }

        index2++;
    }

    while (index3 < playerLastName.Length)
    {
        if (index3 != playerCount)
        {
            newArray3[t] = playerPoints[index3];
            t++;
        }

        index3++;
    }

    return newArray;          
}

static void DeletePlayer(Int32[] playerNumbers, String[] playerLastName, 
    Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS)
{
    int player; // Player number to delete
    int playerindex;//index of the player number in Array

    if (playerCount < MAXPLAYERS)
    {
        player = 
            GetPositiveInteger("\nDelete Player: please enter the player's number");
        playerindex = GetPlayerIndex(player, playerNumbers, playerCount);

        if (playerindex != -1)
        {
            Console.WriteLine(
                "\nDelete Player: Number - {0}, Name - {1}, Points - {2}",
                playerNumbers[playerindex], playerLastName[playerindex], 
                playerPoints[playerindex]);

            Console.WriteLine("Succesfully Deleted");
            Console.WriteLine();
            ProcessDelete(playerNumbers, ref playerCount, playerLastName, 
                playerPoints);
        }
        else
        {
            Console.WriteLine("\nDelete Player: player not found");
        }
    }
    else
    {
        Console.WriteLine("\nDelete Player: the roster is empty");
    }            
}

1 个答案:

答案 0 :(得分:4)

ProcessDelete方法构造新数组newArraynewArray2newArray3。其中,它仅返回newArray,因此newArray2newArray3将被丢弃。当DeletePlayer调用ProcessDelete时,它会忽略返回值,因此newArray也会被丢弃,并且ProcessDelete正文中执行的所有工作都会被浪费。

相关问题