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

时间:2014-11-02 16:48:01

标签: c#

嗨我正在创建一个程序,其中包含三个数组,一个用于姓氏,一个用于得分,一个用于玩家数量,现在我已经拥有了所有数组,所有内容都已完成,但我不确定至于我将如何删除多个数组的条目。到目前为止,这是我的删除播放器方法。正确方向的一些指导真的很有帮助,谢谢你

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

    }
}

1 个答案:

答案 0 :(得分:1)

是否有任何理由在您的情况下使用简单的面向对象方法?

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