ArrayList Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • george r smith

    #1

    ArrayList Question

    I am trying to create an arrayList that contains multiple arrayLists.
    My code attempt is below. The question I have is how can I get away
    from creating another pAttribute list than can be added to pItem.

    As I understand it (and I coded it to test) if I try to use pAttribute again
    by clearing with pAttribute.Clea r because it is a shallow copy the
    pItem[0] is also cleared. I guess I need a "deep copy" but can't find
    any documentation on it.

    So is there any way to pItem[1].Add({100.00,20 0.00}). This will not compile
    but
    it shows what I want to do.

    I might add that I want to use ArrayList because you are not restricted to
    one type
    as in arrays.


    thanks
    grs


    static void Main(string[] args)
    {
    ArrayList pItem = new ArrayList();
    ArrayList pAttribute = new ArrayList();
    for (int i = 101; i < 105; i++)
    {
    pAttribute.Add( i);
    }
    pItem.Add(pAttr ibute);
    PrintValues((Ar rayList)pItem[0]);
    }

    public static void PrintValues(Arr ayList myList)
    {
    IEnumerator myEnumerator = myList.GetEnume rator();
    while (myEnumerator.M oveNext())
    Console.WriteLi ne(myEnumerator .Current);
    }
    }


  • Peter Rilling

    #2
    Re: ArrayList Question

    There are a few ways.

    If you want to follow your suggested attempt, you might need to initialize
    the array like so -- .Add(new int[]{100.00, 200.00}).

    Alternatively, you could like into using the ArrayList.CopyT o(...) method.

    Or try the Object.Memberwi seClone(...) since you are really only interested
    in preserving pAttribute, this might be sufficient.

    "george r smith" <gsmith@budgete xt.com> wrote in message
    news:OXnf$SuqDH A.2636@tk2msftn gp13.phx.gbl...[color=blue]
    > I am trying to create an arrayList that contains multiple arrayLists.
    > My code attempt is below. The question I have is how can I get away
    > from creating another pAttribute list than can be added to pItem.
    >
    > As I understand it (and I coded it to test) if I try to use pAttribute[/color]
    again[color=blue]
    > by clearing with pAttribute.Clea r because it is a shallow copy the
    > pItem[0] is also cleared. I guess I need a "deep copy" but can't find
    > any documentation on it.
    >
    > So is there any way to pItem[1].Add({100.00,20 0.00}). This will not[/color]
    compile[color=blue]
    > but
    > it shows what I want to do.
    >
    > I might add that I want to use ArrayList because you are not restricted to
    > one type
    > as in arrays.
    >
    >
    > thanks
    > grs
    >
    >
    > static void Main(string[] args)
    > {
    > ArrayList pItem = new ArrayList();
    > ArrayList pAttribute = new ArrayList();
    > for (int i = 101; i < 105; i++)
    > {
    > pAttribute.Add( i);
    > }
    > pItem.Add(pAttr ibute);
    > PrintValues((Ar rayList)pItem[0]);
    > }
    >
    > public static void PrintValues(Arr ayList myList)
    > {
    > IEnumerator myEnumerator = myList.GetEnume rator();
    > while (myEnumerator.M oveNext())
    > Console.WriteLi ne(myEnumerator .Current);
    > }
    > }
    >
    >[/color]


    Comment

    • Matt Burland

      #3
      Re: ArrayList Question

      If you want to use pAttribute again without trashing the ArrayList you've
      already created just do:

      pAttribute = new ArrayList();

      The pAttribute reference will now point to a *different* ArrayList and your
      old ArrayList will still be intact and won't be garbage collected because
      it's referenced by pItem[0] (note: if you then null pItem then your old
      ArrayList will be now eligible for garbage collection).
      As you pointed out pAttribute.Clea r() will clear pItem[0] because they are
      both references to the SAME object, you didn't make a copy at all and you
      probably don't want to (whether you know it or not).
      As for using ArrayList instead of arrays, you are wrong about arrays. An
      object[] array can contain a mix of whatever items you want since all
      classes inherit from object. So if you don't need the dynamic resizing
      abilities of the ArrayList, you're better off with object[].


      "george r smith" <gsmith@budgete xt.com> wrote in message
      news:OXnf$SuqDH A.2636@tk2msftn gp13.phx.gbl...[color=blue]
      > I am trying to create an arrayList that contains multiple arrayLists.
      > My code attempt is below. The question I have is how can I get away
      > from creating another pAttribute list than can be added to pItem.
      >
      > As I understand it (and I coded it to test) if I try to use pAttribute[/color]
      again[color=blue]
      > by clearing with pAttribute.Clea r because it is a shallow copy the
      > pItem[0] is also cleared. I guess I need a "deep copy" but can't find
      > any documentation on it.
      >
      > So is there any way to pItem[1].Add({100.00,20 0.00}). This will not[/color]
      compile[color=blue]
      > but
      > it shows what I want to do.
      >
      > I might add that I want to use ArrayList because you are not restricted to
      > one type
      > as in arrays.
      >
      >
      > thanks
      > grs
      >
      >
      > static void Main(string[] args)
      > {
      > ArrayList pItem = new ArrayList();
      > ArrayList pAttribute = new ArrayList();
      > for (int i = 101; i < 105; i++)
      > {
      > pAttribute.Add( i);
      > }
      > pItem.Add(pAttr ibute);
      > PrintValues((Ar rayList)pItem[0]);
      > }
      >
      > public static void PrintValues(Arr ayList myList)
      > {
      > IEnumerator myEnumerator = myList.GetEnume rator();
      > while (myEnumerator.M oveNext())
      > Console.WriteLi ne(myEnumerator .Current);
      > }
      > }
      >
      >[/color]


      Comment

      • george r smith

        #4
        Re: ArrayList Question

        Matt,
        Thanks for the info on object[] array - now that you have told me I
        understand :).
        I do need dynamic so I will keep on the ArrayList. Thanks again.
        grs

        "Matt Burland" <anonymous@disc ussions.microso ft.com> wrote in message
        news:%23ahelhvq DHA.3732@tk2msf tngp13.phx.gbl. ..[color=blue]
        > If you want to use pAttribute again without trashing the ArrayList you've
        > already created just do:
        >
        > pAttribute = new ArrayList();
        >
        > The pAttribute reference will now point to a *different* ArrayList and[/color]
        your[color=blue]
        > old ArrayList will still be intact and won't be garbage collected because
        > it's referenced by pItem[0] (note: if you then null pItem then your old
        > ArrayList will be now eligible for garbage collection).
        > As you pointed out pAttribute.Clea r() will clear pItem[0] because they are
        > both references to the SAME object, you didn't make a copy at all and you
        > probably don't want to (whether you know it or not).
        > As for using ArrayList instead of arrays, you are wrong about arrays. An
        > object[] array can contain a mix of whatever items you want since all
        > classes inherit from object. So if you don't need the dynamic resizing
        > abilities of the ArrayList, you're better off with object[].
        >
        >
        > "george r smith" <gsmith@budgete xt.com> wrote in message
        > news:OXnf$SuqDH A.2636@tk2msftn gp13.phx.gbl...[color=green]
        > > I am trying to create an arrayList that contains multiple arrayLists.
        > > My code attempt is below. The question I have is how can I get away
        > > from creating another pAttribute list than can be added to pItem.
        > >
        > > As I understand it (and I coded it to test) if I try to use pAttribute[/color]
        > again[color=green]
        > > by clearing with pAttribute.Clea r because it is a shallow copy the
        > > pItem[0] is also cleared. I guess I need a "deep copy" but can't find
        > > any documentation on it.
        > >
        > > So is there any way to pItem[1].Add({100.00,20 0.00}). This will not[/color]
        > compile[color=green]
        > > but
        > > it shows what I want to do.
        > >
        > > I might add that I want to use ArrayList because you are not restricted[/color][/color]
        to[color=blue][color=green]
        > > one type
        > > as in arrays.
        > >
        > >
        > > thanks
        > > grs
        > >
        > >
        > > static void Main(string[] args)
        > > {
        > > ArrayList pItem = new ArrayList();
        > > ArrayList pAttribute = new ArrayList();
        > > for (int i = 101; i < 105; i++)
        > > {
        > > pAttribute.Add( i);
        > > }
        > > pItem.Add(pAttr ibute);
        > > PrintValues((Ar rayList)pItem[0]);
        > > }
        > >
        > > public static void PrintValues(Arr ayList myList)
        > > {
        > > IEnumerator myEnumerator = myList.GetEnume rator();
        > > while (myEnumerator.M oveNext())
        > > Console.WriteLi ne(myEnumerator .Current);
        > > }
        > > }
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...