Click here to Skip to main content
15,906,292 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
I would like to extract data from an array.
I have the following:
Arraylist masslistArray = new Arraylist;
This has 5 data points that look like this
for example masslistArray[0] contains {double[6,55]}
masslistArray[1] contains {double[6,232]} and so on

I would like to make another array with data that are in elements 0 and 1
0 and 1 are pairs of data (mass and intensity)
At the end I would like to have another array
with double[2,50] for example
Any suggestions?
Posted
Comments
Albin Abel 19-Feb-11 17:39pm    
double[6,55], what are those numbers, dimensions of the array or data?

1 solution

First of all, I would recommend using System.Collection.Generic.List with proper type argument instead of ArrayList which became obsolete with introduction of generics; this type force you into doing type cases, which is inherently error-prone.

Instead of the type double[2] you use (as I understand, one element is Mass, another is Intensity), you need to use explicit structure with explicit names:

C#
use List = System.Collection.Generic.List<Element>;

struct Element { //give it a semantic name instead, which I don't know, what is it?
    internal Element(double mass, double intensity) { Mass = mass; Intensity = intensity; } 
    internal double Mass;
    internal double Intensity;
} //struct Element

//...

//I abbreviated type name in "using" for convenience:
List masslistArray = new List();

//...
masslistArray[0] = new Element(6, 55); 


Create another array in the same way of the same type or of the similar type but with the element type different similar structure. Then you can access elements of index (within available Length (this is the instance property of List)) or adding elements via Add method. Using indexed access you can assign elements element-to-element.

If you want to have different element types for different array, but there is a semantic way to convert it component-by-component, you can create an implicit convertions operator, for example:

C#
struct Point { 
    //...
} //struct Point

struct Element { 
    public static implicit operator Point(Element value) {
        return new Point(value.Mass, 0, /* something like that... */ );
    } //operator Point
} //struct Element


Without the operator you would have to assign between different types one per component basis (each double member separately, for example), the operator will allow to assign array elements directly.

For arrays of the same type you can also use Array.Copy to make slices and copy part of one array onto another one.

—SA
 
Share this answer
 
Comments
Nish Nishant 19-Feb-11 19:23pm    
Voted 5. This is a comprehensive answer.
Sergey Alexandrovich Kryukov 19-Feb-11 19:28pm    
Thank you, Nishant, hope so...
--SA
Sergey Alexandrovich Kryukov 19-Feb-11 20:25pm    
Shekarchee,

Thank you for accepting my Answer.
Good luck, call again,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900