469,086 Members | 1,280 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,086 developers. It's quick & easy.

Advice on Data structure for storing mixed type elements

4
I am a newbie in programming and I need some advice on storing of multiple types of data together.

My method takes in a String Array as parameter. This string array basically is an array of Chemical elements.

Expand|Select|Wrap|Line Numbers
  1.  
  2. String[] elements = { Calcium, Potassium, Magnesium, Sodium} 
I iterate through each element of the array and call another method that takes in each chemical element as a parameter and calculates 4 values.

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < elements.Length; i++)
  2. {
    ...some lines of code
  3. GetDerivedPropertyValues(Calcium);}
  4.  
Now before I pass the next element as the function parameter, I need to store the 4 values calculated by the method for that corresponding element.

For this reason, I think I may need an ArrayList ( I could be completely wrong though) which stores the first element as a string type i.e the Element Name, and the remaining 4 double values.

Alternatively, I can also create a double array of size 4 to store the 4 calculated values and call each array by the element name

for example,
Expand|Select|Wrap|Line Numbers
  1. double[] calcium = new double[4]
I dont know how to programmatically name the array according to each element of the initial string array.

Please help me and excuse my lack of knowledge if this is a well documented scenario but I simply didn't knew where to look.

Thanks.
Mar 3 '10 #1
4 2373
tlhintoq
3,525 Expert 2GB
I think I would make a class that contains everything together.


Expand|Select|Wrap|Line Numbers
  1. class ChemElem
  2. {
  3.    public string name;
  4.    public string value1;
  5.    public string value2;
  6.    public string value3;
  7.  
  8.    public Calculate()
  9.    {
  10.        // Do work here
  11.    }
  12. }
  13.  
Now you can can make a new ChemElem.
Set the name
Call the Calculate() method which will populate the values.
Then send the ChemElem around your program as needed, and it will have the name and values together.
Mar 3 '10 #2
kwm123
4
Hi tlhintoq , Thanks for your response. I indeed call the calculate method exposed from another class as you have suggested but the thing is I need to store all the values together in a datastructure such as an array or a list as my further calculations require operations such as comparision and other mathematical interactions of related values of different chemical elements´.

So any more suggestions and advices for storing together strings and doubles of each chemical element?
Mar 4 '10 #3
tlhintoq
3,525 Expert 2GB
What was wrong with the suggestion of using a class?
Mar 4 '10 #4
kwm123
4
I think using class is an elegant solution for object oriented approach of calculating multiple values and code reuse. But how about storing those values together for not one but multiple calculations.

For Example, I have a method exposed by a class that takes in the element name and the temperature and calculates multiple temperature dependent properties. For example, when I call a function exposed by a class,
Expand|Select|Wrap|Line Numbers
  1.  GetDerivedProperties(Titanium, 1800)
I get 4 resulting double values:
  1. Partial pressure
  2. Partial Molar Volume
  3. Partial Density
  4. Partial Viscosity
Now, what do you think is the best way to store these 4 values of different elements together? For instance, if I call 7 times, the GetDerivedProperties(element, temp) function for 7 different elements and store the values of each element?
Mar 8 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Dennis Gavrilov | last post: by
reply views Thread by Cees Wesseling | last post: by
reply views Thread by Adam Retter | last post: by
6 posts views Thread by supercomputer | last post: by
2 posts views Thread by SophistiCat | last post: by
5 posts views Thread by Loui Mercieca | last post: by
2 posts views Thread by mirandacascade | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.