Arraylist of Arraylist

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Iapain

    #1

    Arraylist of Arraylist

    Hello,

    I've created an arraylist which consist arraylist ..like

    ArrayList myAL = new ArrayList(); // Main Array List
    ArrayList tmpAL = new ArrayList(); //Temp Array List
    tmpAL.Add(2); //Add int to Temp AL
    tmpAL.Add(3); //Add int to Temp AL
    myAL.Add(tmpAL) ; //Add Temp AL to Main AL
    //(and so on)
    ..
    int[] data = (int[])myAL[0].ToArray(typeof (int); // something like this
    ...i have to access the element in Main arraylist whicj are itself an
    arraylist and then convert them into int[].

    now i have to convert myAL to int[] .. problem is in converting object
    to Array, any hints?

  • Rudderius

    #2
    Re: Arraylist of Arraylist

    Iapain,
    [color=blue]
    > now i have to convert myAL to int[] .. problem is in converting object
    > to Array, any hints?
    >[/color]

    you have to cast the object you get from the main arraylist, which is an
    arraylist, as an arraylist which can be converted to an array

    it sounds hard but is is as simple as this:

    (int[])((ArrayList)ma inAL[i]).ToArray(typeo f(int);

    (ArrayList)main AL[i] means: get the i-th element from main array and
    cast it to an ArrayList, the result is the i-th arraylist in your mainAL.

    hope this will be clare enough

    Comment

    • Kai Brinkmann [MSFT]

      #3
      Re: Arraylist of Arraylist

      ArrayList temp = (ArrayList)myAL[0];
      int[] data = new int[temp.Count]
      temp.CopyTo(dat a);

      Ths last statement throws an InvalidCastExce ption in case the elements in
      the source ArrayList cannot be cast to the type of the destination array.

      --
      Kai Brinkmann [MSFT]

      Please do not send e-mail directly to this alias. This alias is for
      newsgroup purposes only.
      This posting is provided "AS IS" with no warranties, and confers no rights.


      "Iapain" <iapain@gmail.c om> wrote in message
      news:1138231352 .722959.234110@ g47g2000cwa.goo glegroups.com.. .[color=blue]
      > Hello,
      >
      > I've created an arraylist which consist arraylist ..like
      >
      > ArrayList myAL = new ArrayList(); // Main Array List
      > ArrayList tmpAL = new ArrayList(); //Temp Array List
      > tmpAL.Add(2); //Add int to Temp AL
      > tmpAL.Add(3); //Add int to Temp AL
      > myAL.Add(tmpAL) ; //Add Temp AL to Main AL
      > //(and so on)
      > .
      > int[] data = (int[])myAL[0].ToArray(typeof (int); // something like this
      > ..i have to access the element in Main arraylist whicj are itself an
      > arraylist and then convert them into int[].
      >
      > now i have to convert myAL to int[] .. problem is in converting object
      > to Array, any hints?
      >[/color]


      Comment

      • Nick Hounsome

        #4
        Re: Arraylist of Arraylist


        "Iapain" <iapain@gmail.c om> wrote in message
        news:1138231352 .722959.234110@ g47g2000cwa.goo glegroups.com.. .[color=blue]
        > Hello,
        >
        > I've created an arraylist which consist arraylist ..like
        >
        > ArrayList myAL = new ArrayList(); // Main Array List
        > ArrayList tmpAL = new ArrayList(); //Temp Array List
        > tmpAL.Add(2); //Add int to Temp AL
        > tmpAL.Add(3); //Add int to Temp AL
        > myAL.Add(tmpAL) ; //Add Temp AL to Main AL
        > //(and so on)
        > .
        > int[] data = (int[])myAL[0].ToArray(typeof (int); // something like this
        > ..i have to access the element in Main arraylist whicj are itself an
        > arraylist and then convert them into int[].
        >
        > now i have to convert myAL to int[] .. problem is in converting object
        > to Array, any hints?[/color]

        Apart from all the other stuff that people have responded you should never
        right ugly stuff like this.

        The way to go is to write your own matrix class.
        The underlying implementation can use ArrayList but by using your own class
        you will acheive 3 benefits:

        1) It's easier for other to use and understand.
        2) It can be typesafe
        3) It helps you to think more clearly about the types involved than trying
        to stuff it all into a single complicated expression with a lot of casting.
        [For example the casting to ArrayList would have been obvious]


        Comment

        • Iapain

          #5
          Re: Arraylist of Arraylist

          Both Rudderius and Kai suggested an excellent way to use Arraylist of
          Arraylist, Nick i your suggestion is excellent too ..Thanks it
          certainly helped me a lot. I've one more question may be its stupid but
          its happening with me .. If i clear tempAL like below

          ArrayList myAL = new ArrayList(); // Main Array List
          ArrayList tmpAL = new ArrayList(); //Temp Array List
          tmpAL.Add(2); //Add int to Temp AL
          tmpAL.Add(3); //Add int to Temp AL
          myAL.Add(tmpAL) ; //Add Temp AL to Main AL
          tmpAL.Clear() // clear temp AL

          now i am clearing the tmpAL after adding it to MainAL ..would the
          content of MainAL have tmpAL? I've tried and their is no content in
          MainAL ?? any solution?

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Arraylist of Arraylist

            Iapain wrote:[color=blue]
            > Both Rudderius and Kai suggested an excellent way to use Arraylist of
            > Arraylist, Nick i your suggestion is excellent too ..Thanks it
            > certainly helped me a lot. I've one more question may be its stupid but
            > its happening with me .. If i clear tempAL like below
            >
            > ArrayList myAL = new ArrayList(); // Main Array List
            > ArrayList tmpAL = new ArrayList(); //Temp Array List
            > tmpAL.Add(2); //Add int to Temp AL
            > tmpAL.Add(3); //Add int to Temp AL
            > myAL.Add(tmpAL) ; //Add Temp AL to Main AL
            > tmpAL.Clear() // clear temp AL
            >
            > now i am clearing the tmpAL after adding it to MainAL ..would the
            > content of MainAL have tmpAL? I've tried and their is no content in
            > MainAL ?? any solution?[/color]

            You need to be aware of reference type semantics. ArrayLists store
            references to objects, so whatever you do to tmpAL (while it's still
            referring to the same object) is visible via myAL[0], because they're
            both references to the same object. To put this in a slightly simpler
            context (with only one ArrayList involved) if you do:

            ArrayList list1 = new ArrayList();
            ArrayList list2 = list1;

            list2.Add("Hell o");
            Console.WriteLi ne (list1.Count);

            the output will be 1, because the values of list1 and list2 are
            references to the same object.

            Jon

            Comment

            • Iapain

              #7
              Re: Arraylist of Arraylist

              Absolutly correct Jon, thank you guys! i've solved my problem.

              Comment

              Working...