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. Memory and Collections

Memory and Collections

Scheduled Pinned Locked Moved C#
data-structuresperformancehelpquestion
5 Posts 4 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.
  • H Offline
    H Offline
    horacyjr
    wrote on last edited by
    #1

    I need to keep informations in memory, so i am using System.Collections - ArrayList, my structure is a Father and Son and grandson, where each Father can have any Son and each Son can have any grandson . Father and Son and grandson are Arraylist, is there any problem adding then in each other ? this structure is a tree, is there another to do this ? ArrayList father = new ArrayList(); ArrayList son = new ArrayList(); ArrayList grandson = new ArrayList(); grandson.Add("joão"); grandson.Add("MAx"): grandson.Add("Junior"): son.Add(grandson); father.Add(son); thanks

    C 1 Reply Last reply
    0
    • H horacyjr

      I need to keep informations in memory, so i am using System.Collections - ArrayList, my structure is a Father and Son and grandson, where each Father can have any Son and each Son can have any grandson . Father and Son and grandson are Arraylist, is there any problem adding then in each other ? this structure is a tree, is there another to do this ? ArrayList father = new ArrayList(); ArrayList son = new ArrayList(); ArrayList grandson = new ArrayList(); grandson.Add("joão"); grandson.Add("MAx"): grandson.Add("Junior"): son.Add(grandson); father.Add(son); thanks

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Why not a class for each, which carries an arraylist of it's children and one of it's parents ? Ideally, you could give each person an ID, and store the names in a single hashtable by ID, so you can look up names when you need them, but the classes just carry an array of numbers ( nice and efficient, each name is only stored once ). Then you could have a single arraylist of person objects, each of which has it's own ID, and the ID's of it's relatives. Of course, I'm not sure how this would suit you in terms of your searching needs. So Person bob = new Person("Bob"); Person fred = new Person("Fred"); Person Bill = new Person("Bill"); Inside the person class: ArrayList myChildren = new ArrayList(); static Hashtable Names = new Hashtable(); static int IDs = 0; Inside the constructor: this.ID = IDs++; Person.Names[this.ID] = name; Then a method to add a child: public int AddChild(Person child) { myChildren.Add(child.ID); return myChildren.Count; } You could either add grandchildren with a method, or implicitly ( have a GetGrandchildren method that works out who the children of my children are ). Christian Graus - Microsoft MVP - C++

      H 1 Reply Last reply
      0
      • C Christian Graus

        Why not a class for each, which carries an arraylist of it's children and one of it's parents ? Ideally, you could give each person an ID, and store the names in a single hashtable by ID, so you can look up names when you need them, but the classes just carry an array of numbers ( nice and efficient, each name is only stored once ). Then you could have a single arraylist of person objects, each of which has it's own ID, and the ID's of it's relatives. Of course, I'm not sure how this would suit you in terms of your searching needs. So Person bob = new Person("Bob"); Person fred = new Person("Fred"); Person Bill = new Person("Bill"); Inside the person class: ArrayList myChildren = new ArrayList(); static Hashtable Names = new Hashtable(); static int IDs = 0; Inside the constructor: this.ID = IDs++; Person.Names[this.ID] = name; Then a method to add a child: public int AddChild(Person child) { myChildren.Add(child.ID); return myChildren.Count; } You could either add grandchildren with a method, or implicitly ( have a GetGrandchildren method that works out who the children of my children are ). Christian Graus - Microsoft MVP - C++

        H Offline
        H Offline
        horacyjr
        wrote on last edited by
        #3

        Hi Christian Graus thanks for helping i would like to know one more thing, how does an ArrayList works ?it is like a linked node or it is like a common Array ? Thanks

        L J 2 Replies Last reply
        0
        • H horacyjr

          Hi Christian Graus thanks for helping i would like to know one more thing, how does an ArrayList works ?it is like a linked node or it is like a common Array ? Thanks

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          It uses an array in the background :) xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

          1 Reply Last reply
          0
          • H horacyjr

            Hi Christian Graus thanks for helping i would like to know one more thing, how does an ArrayList works ?it is like a linked node or it is like a common Array ? Thanks

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            It uses an array in the background. If the number of items added are more than the number of items in the array, the array is copied, and reallocated to a larger array, usually 2x the size of the original.

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Conversation With a Muslim Judah Himango

            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