尝试从数组中删除单个值
本文关键字:删除 单个值 数组 | 更新日期: 2023-09-27 18:31:17
我正在尝试从所选索引处传递的数组中删除值。基本上是获取显示数组的列表框的选定索引并使用它来删除条目。由于某种原因,当我运行程序时,不会删除任何内容并保持相同。我正在用C#在Visual Studio 2010中编程
public double [] redrawArray(double[] ary, int selectedIndex)
{
double[] redoneArray = new double[ary.GetUpperBound(0)];
for (int i = selectedIndex; i < ary.GetUpperBound(0); i++)
{
redoneArray[i] = ary[i + 1];
}
//redoneArray[ary.GetUpperBound(0)] = 0;
return redoneArray;
}
这是代码的删除按钮部分
private void btnDelete_Click(object sender, EventArgs e)
{
int selectedIndex;
if (this.lstClients.SelectedIndex <= 1)
{
return;
}
//else if ((this.lstClients.SelectedIndex - 2) < arrayIndex)
else
{
selectedIndex = this.lstClients.SelectedIndex - 2;
this.lstClients.Items.RemoveAt(this.lstClients.SelectedIndex);
lbSI.Text = (this.lstClients.SelectedIndex - 2).ToString();
redrawArray(mFirstNameArray, selectedIndex);
redrawArray(mLastNameArray, selectedIndex);
redrawArray(mAgeArray, selectedIndex);
redrawArray(mHeightArray, selectedIndex);
redrawArray(mStartWeightArray, selectedIndex);
redrawArray(mGoalWeightArray , selectedIndex);
redrawArray(mTotalWeeksArray, selectedIndex);
//arrayIndex += -1;
lstClients.Items.Clear();
loadListBox();
}
}
这是我将其加载到列表中的主要代码
private void loadListBox()
{
string currentClient;
int lineNumber = 0;
string formattedName;
string strAvgBMI;
string strLowBMI;
string strHghBMI;
double dStartAvgBMI;
double dStartLowBMI;
double dStartHghBMI;
double dEndAvgBMI;
double dEndLowBMI;
double dEndHghBMI;
lstClients.Items.Add(" CLIENT NAME AGE HEIGHT(in) START WEIGHT START BMI GOAL WEIGHT GOAL BMI WEEKS");
lstClients.Items.Add("================= ===== =========== ============== =========== ============= ========== =========");
for (int index = 0; index < arrayIndex; index++)
{
if (mFirstNameArray[index] == null)
{
continue;
}
lineNumber++;
formattedName = mFirstNameArray[index];
formattedName += " ";
formattedName += mLastNameArray[index];
mStartBMI[index] = calcBMI(mHeightArray[index], mStartWeightArray[index]);
mEndBMI[index] = calcBMI(mHeightArray[index], mGoalWeightArray[index]);
currentClient = index.ToString() + " ";
currentClient += formattedName.PadRight(18) + " ";
currentClient += mAgeArray[index].ToString("##").PadLeft(4) + " ";
currentClient += mHeightArray[index].ToString("##.#0").PadRight(4) + " ";
currentClient += mStartWeightArray[index].ToString("###.0").PadRight(4) + " ";
currentClient += mStartBMI[index].ToString("##.#0").PadRight(4) + " ";
currentClient += mGoalWeightArray[index].ToString("###.0").PadRight(4) + " ";
currentClient += mEndBMI[index].ToString("###.#0").PadRight(4) + " ";
currentClient += mTotalWeeksArray[index].ToString("##").PadRight(4);
lstClients.Items.Add(currentClient);
}
dStartAvgBMI = sumAvg(mStartBMI, arrayIndex);
dStartHghBMI = maxArray(mStartBMI, arrayIndex);
dStartLowBMI = minArray(mStartBMI, arrayIndex);
dEndAvgBMI = sumAvg(mEndBMI, arrayIndex);
dEndHghBMI = maxArray(mEndBMI, arrayIndex);
dEndLowBMI = minArray(mEndBMI, arrayIndex);
strAvgBMI = "";
strHghBMI = "";
strLowBMI = "";
strAvgBMI = " Average: " + dStartAvgBMI.ToString("0#.#0") + " " + dEndAvgBMI.ToString("0#.#0");
strHghBMI = " High: " + dStartHghBMI.ToString("0#.#0") + " " + dEndHghBMI.ToString("0#.#0");
strLowBMI = " Low: " + dStartLowBMI.ToString("0#.#0") + " " + dEndLowBMI.ToString("0#.#0");
lstClients.Items.Add(strAvgBMI);
lstClients.Items.Add(strHghBMI);
lstClients.Items.Add(strLowBMI);
}
我强烈建议改用List<double>
,因为它具有更优雅和有效的添加和删除项的策略。
您的算法存在重大问题,因为您实际上没有使用selectedIndex
来过滤掉"已删除"的项目。我认为它应该看起来像这样
public double[] redrawArray(double[] ary, int selectedIndex)
{
double[] redoneArray = new double[ary.GetUpperBound(0)];
int i = 0;
for (; i < selectedIndex; i++)
{
redoneArray[i] = ary[i];
}
for (; i < redoneArray.Length; i++)
{
redoneArray[i] = ary[i + 1];
}
return redoneArray;
}
甚至更好:
public double[] redrawArray(double[] ary, int selectedIndex)
{
double[] redoneArray = new double[ary.GetUpperBound(0)];
Array.Copy(ary, redoneArray, selectedIndex);
Array.Copy(ary, selectedIndex + 1,
redoneArray, selectedIndex, redoneArray.Length - selectedIndex);
return redoneArray;
}
更新
然而,真正的问题是你redrawArray
方法返回一个新数组,而不是修改现有数组。您必须使用将结果数组赋值回变量,如下所示:
mFirstNameArray = redrawArray(mFirstNameArray, selectedIndex);
mLastNameArray = redrawArray(mLastNameArray, selectedIndex);
mAgeArray = redrawArray(mAgeArray, selectedIndex);
mHeightArray = redrawArray(mHeightArray, selectedIndex);
mStartWeightArray = redrawArray(mStartWeightArray, selectedIndex);
mGoalWeightArray = redrawArray(mGoalWeightArray , selectedIndex);
mTotalWeeksArray = redrawArray(mTotalWeeksArray, selectedIndex);
您是从selectedIndex
开始的,但新数组不包含任何源数组。这使得索引之前的所有内容都0
。这可以通过Array.Copy
来解决。
public static double[] redrawArray(double[] ary, int selectedIndex) {
double[] redoneArray = new double[ary.GetUpperBound(0)];
Array.Copy(ary, redoneArray, ary.GetUpperBound(0)); // copy the source into the destination minus one..
for (int i = selectedIndex; i < ary.GetUpperBound(0); i++) {
redoneArray[i] = ary[i + 1];
}
return redoneArray;
}
用法示例:
double[] arr = new double[] {2, 4, 6};
// remove first
arr = redrawArray(arr, 0); // {4, 6}
// remove second
arr = redrawArray(arr, 1); // {2, 6}
// etc..
如果你
想要一个可以从中删除内容的数组,那么最好使用类似的东西 List<double>
,这将为您省去很多麻烦。
然后要删除特定索引,只需调用 .RemoveAt(index)
如果你仍然想在外部使用数组,你可以通过使用array.ToList()
函数来"作弊",为自己获取一个列表,删除你想要的任何内容,然后将其.toArray()
回去。是的,它的效率很低,但我认为你目前所做的并没有那么快。
public double[] RedrawArray(double[] ary, int selectedIndex)
{
var lst = new List<double>(ary);
lst.RemoveAt(selectedIndex);
return lst.ToArray();
}
有了 Linq,我想你可以:
public double[] RedrawArray(double[] ary, int selectedIndex)
{
return ary.Where((d, i) => i!=selectedIndex).ToArray();
}
或:
public void RedrawArray(ref double[] ary, int selectedIndex)
{
ary = ary.Where((d, i) => i!=selectedIndex).ToArray();
}
取决于调用方法时最方便的语法。
请注意,无论哪种情况,传递给此方法的数组都不应在方法运行时由其他线程修改。在ref
情况下,如果传递了字段或捕获的变量,则其他线程不得重新分配该变量。