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