数组如何删除多个条目 [英] Arrays how to delete multiple entries

查看:82
本文介绍了数组如何删除多个条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

推荐答案

好的,你现在的情况这是一个直接的迹象,你需要对你的代码进行一些重新考虑。



在我走得更远之前,你提出的问题可以很容易地回答,而且有人在这里论坛可能也会这样做。但是,我相信这只会解决短期/即时问题,你很快就会遇到另一个挑战。



即使数据(数字)也是如此,名称和点)是相互关联的,你没有将它们用作单个实体但是单独的项目然后只依赖于数组索引。



相反,你应该先专注于如何拥有一个更好的结构/课程,可以为您存储这些信息。



您可以开始这样的事情:



OK, the situation you currently have is a direct sign that you need some re factoring in your code.

And before I go further, the question you have asked can be answered very easily and someone in this forum might do as well. However, I am sure that would just solve the short/immediate problem and you would be faced with another challenge soon.

The thing is that even though the data (number, name and point) is interrelated, you have not used them as a single entity but separate items and then just relying on the array index.

Instead, you should first focus on how to have a better structure/class which can store this information for you.

You could start something like this:

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





这为您提供了一个存储玩家信息的对象。如果你明天有一个新的房产,比如玩家的年龄等,你只需要在这里添加它作为起点。



同样,当你处理删除时,你可以开始这样的事情:





This gives you a object which stores the information for a player. If you have a new property tomorrow such as Player's age etc., you just need to add it here as a starting point.

Similarly, when you are processing deletes, you could start something like this:

public void ProcessDelete(List<player> players)
{
	//your logic to delete a certain player here
}




public void DeletePlayer(Player player)
{
	//your logic here
}



我希望这有助于您进一步思考。祝你好运!


I hope this helps you to think further. Good luck!


这篇关于数组如何删除多个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆