从数组中提取数据................................................... ................................ [英] Extracting data from an array.............................................................................

查看:80
本文介绍了从数组中提取数据................................................... ................................的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我想从数组中提取数据.
我有以下内容:
Arraylist masslistArray =新的Arraylist;
它有5个看起来像这样的数据点
例如masslistArray [0]包含{double [6,55]}
masslistArray [1]包含{double [6,232]},依此类推

我想用元素0和1中的数据制作另一个数组
0和1是成对的数据(质量和强度)
最后,我想拥有另一个数组
以double [2,50]为例
有什么建议吗?

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?

推荐答案

首先,我建议使用带有正确类型参数的System.Collection.Generic.List而不是ArrayList,后者由于引入了泛型而变得过时;这种类型会迫使您进行类型检查,这本来就是容易出错的.

代替使用类型double [2](据我所知,一个元素是质量,另一个元素是强度),您需要使用带有显式名称的显式结构:

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:

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); 



以相同类型或相似类型的相同方式创建另一个数组,但元素类型具有不同的相似结构.然后,您可以访问index的元素(在可用的Length(这是List的实例属性内))或通过Add方法添加元素.使用索引访问,您可以将元素分配为元素.

如果要为不同的数组使用不同的元素类型,但是有一种语义方式可以逐个组件地对其进行转换,则可以创建一个隐式转换运算符,例如:



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:

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



没有运算符,您将不得不在不同类型之间为每个组件分配一个类型(例如,每个双成员分别),运算符将允许直接分配数组元素.

对于相同类型的数组,还可以使用Array.Copy进行切片并将一个数组的一部分复制到另一数组上.

—SA



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


这篇关于从数组中提取数据................................................... ................................的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆