extracting from an ArrayList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • norulzs
    New Member
    • Jan 2008
    • 9

    extracting from an ArrayList

    Hi,
    In a attempt to create a dynamic multi diminsion array i am using a ArrayList object as i do not know how many dimension i will need during runtime. My code is

    ArrayList mySubArray = new ArrayList();
    ArrayList myArray = new ArrayList();

    //while loop which can have any amount of iterations

    mySubArray.Add( sName);
    mySubArray.Add( sTitle);
    mySubArray.Add( sAddress);

    myArray.Add(myS ubArray.Clone() );

    mySubArray.Clea r();

    Loop

    QUESTION
    how do i extract the nodes from my subArray object that is within the myArray ArrayList.

    Thanks.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by norulzs
    Hi,
    In a attempt to create a dynamic multi diminsion array i am using a ArrayList object as i do not know how many dimension i will need during runtime. My code is

    ArrayList mySubArray = new ArrayList();
    ArrayList myArray = new ArrayList();

    //while loop which can have any amount of iterations

    mySubArray.Add( sName);
    mySubArray.Add( sTitle);
    mySubArray.Add( sAddress);

    myArray.Add(myS ubArray.Clone() );

    mySubArray.Clea r();

    Loop

    QUESTION
    how do i extract the nodes from my subArray object that is within the myArray ArrayList.

    Thanks.
    Just do myArray[i] . That will return an ArrayList. You then call list[i] on the returned ArrayList to get the "nodes".

    However, that design is very bad and cumbersome. Better have a Details class that has sName, title, adddress e.t.c as properties.
    You'll then need only one ArrayList of type ArrayList<Detai ls>.
    Object oriented design really makes the programming much easier.

    Comment

    • norulzs
      New Member
      • Jan 2008
      • 9

      #3
      Originally posted by r035198x
      Just do myArray[i] . That will return an ArrayList. You then call list[i] on the returned ArrayList to get the "nodes".

      However, that design is very bad and cumbersome. Better have a Details class that has sName, title, adddress e.t.c as properties.
      You'll then need only one ArrayList of type ArrayList<Detai ls>.
      Object oriented design really makes the programming much easier.
      Is this what you mean:

      public class MemberDetails
      {
      public string sName = null;
      public string sTitle = null;
      public string sAddress = null;

      public MemberDetails()
      {
      }

      public MemberDetails( string Name,
      string Title,
      string Address,)
      {
      this.sName = Name;
      this.sTitle = Title;
      this.sAddress = sDoc;
      }
      }


      //loop
      myArray.Add(new MemberDetails(s Name, sTitle, sAddress));
      //loop end

      //get back nodes
      foreach (MemberDetails member in myArray)
      {
      string memberName = member.sName;
      string memberTitle = member.sTitle;
      string memberAddress = member.sAddress
      }

      Thanks for making it clear

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Yep, you've got it.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          If you are talking a large amount of data that you might want to do "work" on, you might consider using a DataTable.
          Give yourself a "column" for each value you want to track, then make a new "row" for each entry.

          Comment

          • norulzs
            New Member
            • Jan 2008
            • 9

            #6
            Originally posted by Plater
            If you are talking a large amount of data that you might want to do "work" on, you might consider using a DataTable.
            Give yourself a "column" for each value you want to track, then make a new "row" for each entry.
            So what are the major issues/drawbacks for using my current way if lets says i had 2000 rows to process?

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              I am not sure if there is much difference in "resource" usage.
              Just a difference in how you access the data.

              Also, I am not positive on this, but I *think* there is a way you can do SQL queries against a dataset. (Like finding everyone who's name starts with a "P" or something)

              Comment

              • norulzs
                New Member
                • Jan 2008
                • 9

                #8
                OK cool, thanks for your advice/help

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by norulzs
                  So what are the major issues/drawbacks for using my current way if lets says i had 2000 rows to process?
                  You'd need to be calling ArrayList.TrimT oSize otherwise you'd be wasting space because arraylist's capacity is always >= arraylist's count.
                  The most memory efficient approach would probably be to use a struct.
                  However, all this depends (as Plater said) on what you want to do with the data.

                  Comment

                  • norulzs
                    New Member
                    • Jan 2008
                    • 9

                    #10
                    I'm just passing the data into a stored procedure one by one, so I don't need to run any queries on the dataset before hand, but i think your right it would be better to use a struct instead.

                    Comment

                    Working...