单个数组的多维数组操作...... [英] Multi-Dimentional array manipulation with single arrays...

查看:84
本文介绍了单个数组的多维数组操作......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上寻找一个例子,似乎没有看到我正在尝试做的事情。



我有一个二维整数数组,其中第一个数字是从零开始到20(或更小)的顺序号,第二个是4位数整数似乎是随机的但是可重复的,每个最多有120个数据点。



我正在尝试使用带有零和重复的单维数组(720个元素)构建连续的数据迭代,然后我将从中删除重复和零来自二维数组的任何相同数字的出现,然后将清理后的数组添加到二维数组的下一个连续数字。最终的数组将具有不同的数据点。 (我有一个很大的列表类,我从中获取数据点。)



能够从多维数组中取出其中一个单个数组也可能很有用。我正在使用Linq,以为我之前看过这样的例子,但我现在似乎无法找到它。任何建议?

I've been looking for an example on the web and don't seem to see anything like what I'm trying to do.

I have a 2 dimensional integer array, where the first number is a sequential number starting at zero and going to 20 (or less) and the second is a 4 digit integer that would seem to be random but is repeatable having up to 120 data points each max.

I'm trying to build sequential iterations of data using a single dimensional array (720 elements) with zeros and duplicates, that I will then remove duplicates and zeros from and any occurrences of identical numbers from the two dimensional array and then add the cleaned up array to the next sequential number of the two dimensional array. The final array will have distinct data points. (I have a large list class that I'm getting the data points from.)

Being able to take one of these single arrays out of the multidimensional array might also be useful. I'm using Linq, and thought I saw this sort of example before, but I just can't seem to find it now. Any suggestions?

推荐答案

所有这些问题都以一种非常简单的方式解决,但有一些简单但不那么明显的技术。



这是包装类,它基于数组元素的相同物理位置,同时表现得像2D和1D数组。引擎盖实现阵列是第一个:

All such problems are solved in a really simple way, but there are some simple but not so obvious techniques.

Here is the wrapper class which behaves like a 2D and 1D array at the same time, based on the same physical locations of array elements. The under-the-hood implementation array is the 1D one:
interface IArray2D<T> {
    T this[int x, int y] { get; set; }
} //Array2D

public class DualUseArray<T> : IArray2D<T> {

    public DualUseArray(int sizeX, int sizeY ) {
        this.implementation = new T[sizeX * sizeY];
        this.width = sizeX;
    } //DualUseArray

    public T this[int x, int y] {
        get { return implementation[GetIndex(x, y)]; }
        set { implementation[GetIndex(x, y)] = value; }
    } //this

    int GetIndex(int x, int y) {
        return y * width + x;
    } //GetIndex

    public T this[int index] {
        get { return implementation[index]; }
        set { implementation[index] = value; }
    } //this

    T[] implementation;
    int width;

} //DualUseArray





这是两用的方式:



Here is the double-use way:

DualUseArray<string> myArray = new DualUseArray<string>(12, 100);
myArray[120] = "120";
myArray[12, 10] = "12, 10";
string value = myArray[120];





有这个想法吗?



注意使用的辅助接口。看起来这是在同一类型中引入多个索引属性 this 的唯一方法。当然,所有索引属性的签名都应该不同。



您可以通过某些方式改进此解决方案:您可以检查2D索引的范围约束,如以及检查构造函数参数的有效性,您可以添加构造函数将数组元素初始化为相同的值或其他一些方式,也许是其他方式。以同样的方式,您可以引入3D,4D等表示......



-SA



Got the idea?

Pay attention for the auxiliary interface used. It looks like this is the only way to introduce more than one indexed property "this" in the same type. Of course all indexed properties should be different in their signatures.

You can improve this solution in some ways: you can check up the range constraints for the 2D indices, as well as check up validity of constructor parameters, you can add constructors initializing the array elements to same value or some other ways, maybe something else. In the same way, you can introduce representations like 3D, 4D, etc...

—SA

如果我正确认识到你的问题,那么你可能正在寻找一种方法将你的mufti维数组分割成单个数组。是吗?

那么这2个肯定会帮助你..

http:// stackoverflow .com / questions / 4801990 / how-to-a-dimension-slice-from-a-multidimensional-array [ ^ ]

http://stackoverflow.com/questions/9322479/converting-two-dimensional-array- to-one-dimensional-in-c [ ^ ]
If i recognize your problem correctly then possibly you are looking for a way to split your mufti-dimensional array to single arrays.Is it?
Then these 2 will definitely help you..
http://stackoverflow.com/questions/4801990/how-to-get-a-dimension-slice-from-a-multidimensional-array[^]
http://stackoverflow.com/questions/9322479/converting-two-dimensional-array-to-single-dimensional-in-c[^]


这篇关于单个数组的多维数组操作......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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