如何清除数组中的选定元素

时间:2014-11-17 17:02:23

标签: c#

您好我创建的程序包含三个数组,一个用于姓氏,一个用于得分,一个用于玩家编号。我做了一个删除方法,假设当用户输入一个玩家编号时删除玩家编号,姓氏和输入的玩家编号的分数。但是当我输入一个玩家编号来删除玩家数据时它会清除整个阵列。希望能够清除玩家编号,姓氏和输入的玩家编号的分数,但只是这些项而不是整个阵列。我不确定如何做到这一点。

正确方向的一些帮助或指导真的很有帮助,谢谢你

static void ProcessDelete( Int32[] playerNumbers, ref Int32 playerCount,  String[] playerLastName,  Int32[] playerPoints)
    {

        Int32[] newArray = new Int32[playerNumbers.Length]; String[] newArray2 = new String[playerLastName.Length]; Int32[] newArray3 = new Int32[playerPoints.Length];

        int index = Array.IndexOf(playerNumbers, 0);


        {
            for (int i = 0; i < playerNumbers.Length; i++)

                playerNumbers[i] = 0;

        }


        for (int i = 0; index < playerLastName.Length; index++)


                playerLastName[i] = " ";



        for (int i = 0; i < playerPoints.Length; i++)

            playerPoints[i] = 0;

    }



    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 :(得分:0)

正确的答案是做Habib所建议的,并使用这三个属性创建一个Player类,并使用一个列表。

但是,要回答你问的问题,你的问题是你用循环遍历数组中的每个元素并重置所有内容。如果您只想清除单个播放器的数组,则ProcessDelete方法应使用您在DeletePlayer方法中获取的索引。

static void ProcessDelete( Int32[] playerNumbers, String[] playerLastName,  Int32[] playerPoints, Int32 playerIndex)
{
  playerNumbers[playerIndex] = 0;
  playerLastName[playerIndex] = " ";
  playerPoints[playerIndex] = 0;
}