Removing objects in array less than 20.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashraf02
    New Member
    • Feb 2008
    • 53

    Removing objects in array less than 20.

    I am a complete newbie in c# programming and have run in to a problem. can someone please help me.

    basically i want to remove all widgets that are less than 20 in length from the array list. here's the code if someone can help it would be much appreciated.

    heres the code.

    Code:
    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 ArrayList GetRidOfTheSmallWidgets(ArrayList colBoxesOfWidgets){
    
    
                return colBoxesOfWidgets;
    
            }
        }
    
        class BoxOfWidgets
        {
            public string boxType;
            public ArrayList colWidgets;
    
            public BoxOfWidgets(string newBoxType)
            {
                boxType = newBoxType;
                colWidgets = new ArrayList();
            }
        }
    
        class Widget
        {
            public string name;
            public float length;
    
            public Widget(string newName, float newLength)
            {
                this.name = newName;
                this.length = newLength;
            }
        }
    
    
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This is C# so I'm moving this to the .NET forum.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      I hate to say it, but this is beginner OOP logic.

      Use a nested loop. The outer loop loops through the collection of BoxOfWidgets objects. The inner loops through the colWidgets in each BoxOfWidgets. You can .Remove the ones that are too short. Just remember that when you remove one, make sure you remember to do iterator--. Because when you remove the current index, the next one becomes the current one, and you don't want to skip it.
      For example:
      Code:
      ArrayList arrList = new ArrayList();
      //assume I load it with 20 or so strings
      //now I want to get rid of the ones that are blank.
      for(int i=0; i<arrList.Count; i++)
      {
        string temp = (string)arrList[i];
        if(temp.Equals(""))
        {
          arrList.RemoveAt(i);
          i--;
        }
      }
      That's the logic for the inner loop. For the outer, just loop straight through your list.

      Comment

      Working...