Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Holding ArrayLists in an ArrayList

Holding ArrayLists in an ArrayList

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amadeonMk
    wrote on last edited by
    #1

    Hi,i try to hold a number arraylists in a single main arraylist.But i think because of a referance problem, everytime i add a new arraylist, previous added lists became same with last added one;all items shown as the last arraylist's items.is anyone knows the solution or a different way to hold arraylists? Thanks

    A 1 Reply Last reply
    0
    • A amadeonMk

      Hi,i try to hold a number arraylists in a single main arraylist.But i think because of a referance problem, everytime i add a new arraylist, previous added lists became same with last added one;all items shown as the last arraylist's items.is anyone knows the solution or a different way to hold arraylists? Thanks

      A Offline
      A Offline
      A Wegierski
      wrote on last edited by
      #2

      Array is an object. You are trying something like: ArrayList a1,a2; a1=new ArrayList(); a2=new ArrayList(); ... a1.Add(a2); ... a1.Add(a2); // thesame array should be: ArrayList a1,a2; a1=new ArrayList(); a2=new ArrayList(); ... a1.Add(a2); a2=new ArrayList(); ... a1.Add(a2); // another array Hi, AW

      A 1 Reply Last reply
      0
      • A A Wegierski

        Array is an object. You are trying something like: ArrayList a1,a2; a1=new ArrayList(); a2=new ArrayList(); ... a1.Add(a2); ... a1.Add(a2); // thesame array should be: ArrayList a1,a2; a1=new ArrayList(); a2=new ArrayList(); ... a1.Add(a2); a2=new ArrayList(); ... a1.Add(a2); // another array Hi, AW

        A Offline
        A Offline
        amadeonMk
        wrote on last edited by
        #3

        Thanks but a have already use this.maybe it would be more clear if i send my code private ArrayList ALMainList=new ArrayList(); private ArrayList ALSendedList; ... MyArrayList.Add("al1"); CatchSendedList(MyArrayList) ... MyArrayList.Add("al2"); CatchSendedList(MyArrayList) ... MyArrayList.Add("al3"); CatchSendedList(MyArrayList) ... public void CatchSendedList(ArrayList ALSendedL) { ALSendedList=new ArrayList(); ALSendedList=ALSendedL; ALMainList.Add(ALSendedList); } when i use this code i take the result: ALMainList-item0 -> al3 ALMainList-item1 -> al3 ALMainList-item2 -> al3

        J A 2 Replies Last reply
        0
        • A amadeonMk

          Thanks but a have already use this.maybe it would be more clear if i send my code private ArrayList ALMainList=new ArrayList(); private ArrayList ALSendedList; ... MyArrayList.Add("al1"); CatchSendedList(MyArrayList) ... MyArrayList.Add("al2"); CatchSendedList(MyArrayList) ... MyArrayList.Add("al3"); CatchSendedList(MyArrayList) ... public void CatchSendedList(ArrayList ALSendedL) { ALSendedList=new ArrayList(); ALSendedList=ALSendedL; ALMainList.Add(ALSendedList); } when i use this code i take the result: ALMainList-item0 -> al3 ALMainList-item1 -> al3 ALMainList-item2 -> al3

          J Offline
          J Offline
          Julian Bucknall MSFT
          wrote on last edited by
          #4

          You're just copying references around. When you write

          ALSendedList=ALSendedL;

          you're merely copying the reference to the array list, you are not copying the array list itself. You need to think about copying the items in the array list instead. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

          1 Reply Last reply
          0
          • A amadeonMk

            Thanks but a have already use this.maybe it would be more clear if i send my code private ArrayList ALMainList=new ArrayList(); private ArrayList ALSendedList; ... MyArrayList.Add("al1"); CatchSendedList(MyArrayList) ... MyArrayList.Add("al2"); CatchSendedList(MyArrayList) ... MyArrayList.Add("al3"); CatchSendedList(MyArrayList) ... public void CatchSendedList(ArrayList ALSendedL) { ALSendedList=new ArrayList(); ALSendedList=ALSendedL; ALMainList.Add(ALSendedList); } when i use this code i take the result: ALMainList-item0 -> al3 ALMainList-item1 -> al3 ALMainList-item2 -> al3

            A Offline
            A Offline
            A Wegierski
            wrote on last edited by
            #5

            ... or use CopyTo() method (not Clone()) Hi, AW

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups