尝试从数组中删除单个值

时间:2013-09-15 07:12:21

标签: c#

我正在尝试从所选索引处传递的数组中删除一个值。基本上采用显示数组的列表框的选定索引并使用它来删除条目。出于某种原因,当我运行程序时,没有任何内容被删除并保持不变。我正在使用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);
}

5 个答案:

答案 0 :(得分:2)

您从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..

答案 1 :(得分:2)

我强烈建议您使用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方法返回 new 数组而不是修改现有数组。您必须使用将结果数组分配回变量,如下所示:

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

答案 2 :(得分:1)

如果你想要一个可以删除内容的数组,那么最好使用List<double>之类的内容,这样可以省去很多麻烦。

然后要删除特定索引,只需致电.RemoveAt(index)

如果您仍想在外部使用Arrays,可以使用array.ToList()函数“欺骗”自己的列表,删除您想要的任何内容,然后再.toArray()。是的它效率很低,但我不认为你现在正在做的事情是那么快。

答案 3 :(得分:0)

public double[] RedrawArray(double[] ary, int selectedIndex)
{  
    var lst = new List<double>(ary);
    lst.RemoveAt(selectedIndex);
    return lst.ToArray();
}

答案 4 :(得分:0)

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情况下,如果传递了字段或捕获的变量,则不能由其他线程重新分配该变量。