josh187
josh187

Reputation: 65

Arrays how to delete entries in multiple arrays?

Hi I'm creating a program that holds three arrays one for the persons last name, one for the points scored and one for the player number, now I've got all the arrays and everything done but I'm not sure as to how I'm going to delete an entry for multiple arrays. so far this is what I have for my delete player method. Some guidance in the right direction would really help please and thank you

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

    }
}

Upvotes: 0

Views: 95

Answers (1)

PiotrWolkowski
PiotrWolkowski

Reputation: 8782

Is there any reason a simple Object Oriented approach wouldn't work in your case?

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

As in the comments to the question. You can have a simple data structure to keep player's data and some more user friendly collection to manage a group of players.

Then you can use it like that:

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

Upvotes: 1

Related Questions