数组如何删除多个数组中的项

本文关键字:数组 删除 何删除 | 更新日期: 2023-09-27 18:28:52

嗨,我正在创建一个程序,它包含三个数组——一个用于人名,一个用于得分,一个为玩家编号,现在我已经完成了所有数组和所有操作,但我不确定如何删除多个数组的条目。到目前为止,这就是我的删除播放器方法。一些正确方向的指导将非常有帮助,请并感谢

  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");
        }
    }
}

数组如何删除多个数组中的项

简单的面向对象方法在您的情况下不起作用有什么原因吗?

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

正如在对问题的评论中。你可以有一个简单的数据结构来保存玩家的数据,也可以有一些更用户友好的收集来管理一组玩家。

然后你可以这样使用它:

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