删除方法不删除 [英] delete method not deleting
本文介绍了删除方法不删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我制作了一个包含三个阵列的程序:一个用于姓氏,一个用于得分,一个用于玩家号码。现在,我得到了所有的数组和一切,但由于某种原因我的ProcessDelete方法没有删除数据
任何帮助将不胜感激
Hi, I made 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 got all the arrays and everything done but for some reason my ProcessDelete method Isn't deleting the data
Any help would be appreciated
static Int32[] ProcessDelete(Int32[] playerNumbers, ref Int32 playerCount, String[] playerLastName, Int32[] playerPoints )
{
Int32[] newArray = new Int32[playerNumbers.Length - 1]; String[] newArray2 = new String[playerLastName.Length - 1]; Int32[] newArray3 = new Int32[playerPoints.Length - 1];
int index = 0;
int index2 = 0;
int index3 = 0;
int j = 0;
int k = 0;
int t = 0;
while (index < playerNumbers.Length)
{
if (index != playerCount)
{
newArray[j] = playerNumbers[index];
j++;
}
index++;
}
while (index2 < playerLastName.Length)
{
if (index2 != playerCount)
{
newArray2[k] = playerLastName[index2];
k++;
}
index2++;
}
while (index3 < playerLastName.Length)
{
if (index3 != playerCount)
{
newArray3[t] = playerPoints[index3];
t++;
}
index3++;
}
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();
ProcessDelete(playerNumbers, ref playerCount, playerLastName, playerPoints);
}
}
else
Console.WriteLine("\nDelete Player: player not found");
}
else
Console.WriteLine("\nDelete Player: the roster is empty");
}
}
}
推荐答案
我会这样做,找到索引并使用数组发送并删除它。
I would do as below, find the index and send with array and delete it.
void Main()
{
int[] test = ProcessDelete(new int[]{ 1, 3, 4, 9, 2 }, 3);
//test: 1,3,4,2
}
int[] ProcessDelete(int[] playerNumbers, int index)
{
if(playerNumbers!=null && playerNumbers.Length>0 && index>-1)
playerNumbers = playerNumbers.Where((val, idx) => idx != index).ToArray();
return playerNumbers;
}
你最好使用List而不是数组,然后你可以简单地调用删除 [ ^ ]按索引删除项目的方法。
You better use List instead of array, then you can simply call RemoveAt[^] method to remove item by index.
这篇关于删除方法不删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文