如何在c#中从boxofwidgets的arraylist中搜索和删除较低的值

时间:2012-04-26 18:17:43

标签: c# arraylist


如何使用c#中的“GetRidOfTheSmallWidgets”方法搜索“BoxOfW​​idgets”的数组列表并删除值小于20的小部件,而不使用foreach或任何循环。

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList colBoxesOfWidgets = new ArrayList();

      colBoxesOfWidgets.Add(new BoxOfWidgets("Cardboard"));
      ((BoxOfWidgets)colBoxesOfWidgets[0]).colWidgets.Add(new Widget("The blue widget", 12));
      ((BoxOfWidgets)colBoxesOfWidgets[0]).colWidgets.Add(new Widget("The red widget", 15));
      ((BoxOfWidgets)colBoxesOfWidgets[0]).colWidgets.Add(new Widget("The silver widget", 6));
      ((BoxOfWidgets)colBoxesOfWidgets[0]).colWidgets.Add(new Widget("The green widget", 52));


      colBoxesOfWidgets.Add(new BoxOfWidgets("Metal"));
      ((BoxOfWidgets)colBoxesOfWidgets[1]).colWidgets.Add(new Widget("The gold widget", 9));
    ((BoxOfWidgets)colBoxesOfWidgets[1]).colWidgets.Add(new Widget("The orange widget", 115));
    ((BoxOfWidgets)colBoxesOfWidgets[1]).colWidgets.Add(new Widget("The pink widget", 1));


    colBoxesOfWidgets.Add(new BoxOfWidgets("Metal"));
    ((BoxOfWidgets) colBoxesOfWidgets[2]).colWidgets.Add(new Widget("The grey widget", 12));
    ((BoxOfWidgets) colBoxesOfWidgets[2]).colWidgets.Add(new Widget("The black widget", 15));
    ((BoxOfWidgets) colBoxesOfWidgets[2]).colWidgets.Add(new Widget("The white widget", 19));
    ((BoxOfWidgets) colBoxesOfWidgets[2]).colWidgets.Add(new Widget("The brown widget", 60));
    ((BoxOfWidgets) colBoxesOfWidgets[2]).colWidgets.Add(new Widget("The peach widget", 16));

    GetRidOfTheSmallWidgets(colBoxesOfWidgets);
}

    public static ArrayList GetRidOfTheSmallWidgets(ArrayList colBoxesOfWidgets)
    {
        //It should remove all widgets that have lengths lower than 20.
        return colBoxesOfWidgets;
    }

1 个答案:

答案 0 :(得分:0)

如果你喜欢搞笑,你可以使用if和goto语句。