Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数组如何删除多个数组中的条目?_C# - Fatal编程技术网

C# 数组如何删除多个数组中的条目?

C# 数组如何删除多个数组中的条目?,c#,C#,大家好,我正在创建一个程序,其中包含三个数组,一个用于人名,一个用于得分,一个用于球员编号,现在我已经完成了所有数组和所有操作,但我不确定如何删除多个数组的条目。到目前为止,这就是我的delete-player方法。在正确的方向上提供一些指导真的会很有帮助,请和谢谢 static Int32[] ProcessDelete(Int32[] playerNumbers, String[] playerLastName, Int32[] playerPoints, ref Int32 playe

大家好,我正在创建一个程序,其中包含三个数组,一个用于人名,一个用于得分,一个用于球员编号,现在我已经完成了所有数组和所有操作,但我不确定如何删除多个数组的条目。到目前为止,这就是我的delete-player方法。在正确的方向上提供一些指导真的会很有帮助,请和谢谢

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

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

                index++;
            }

            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();

                    }
                }
                else
                    Console.WriteLine("\nDelete Player: player not found");
            }
            else
                Console.WriteLine("\nDelete Player: the roster is empty");
        }

    }
}
static Int32[]ProcessDelete(Int32[]playerNumber,String[]playerLastName,Int32[]playerPoints,ref Int32 playerCount)
{
Int32[]newArray=newint[playerNumber.Length-1];
int指数=0;
int j=0;
while(索引
简单的面向对象方法在您的案例中不起作用有什么原因吗

public class Player
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Number { get; set; }
    public int Points { get; set; }
}

public class TeamManager
{
    List<Player> players;

    public TeamManager()
    {
        this.players = new List<Player>();
    }

    public void Add(Player player)
    {
        this.players.Add(player);
    }

    public bool Delete(Player player)
    {
        if (this.players.Contains(player))
            return this.players.Remove(player);
        else
            return false;
    }
}

可能数组不适合您的情况。不要为此使用数组,请定义一个类来表示玩家的相关数据。一个
player
类将相关信息保存在一起,一个列表将更容易管理多个玩家
class Program
{
    static void Main(string[] args)
    {
        var player1 = new Player
        {
            Id = 1,
            Name = "Mike",
            Number = 13,
            Points = 5,
        };

        var team = new TeamManager();
        team.Add(player1);
        team.Delete(player1);
    }
}