C#数组最大 [英] C# Array Maximum

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

问题描述

  • 在我有一个名为 ARR1 ARR2 在C#2阵列。 他们是完全相同的尺寸......
  • I have 2 arrays named Arr1 and Arr2 in C#. They are of the exact same dimensions...

我需要得到ARR1以最大的ARR2元素与既定指标开头的元素...

I need to get the element of Arr1 corresponding to maximum of elements in Arr2 beginning with given indices ...

例如
获取 ARR2 [1,10,3,I,J] 所有,<$的最大的指数C $ C>Ĵ
返回 ARR1 [1,10,3,I,J]

e.g
Get indices of the max of Arr2 [ 1 , 10 , 3 , i , j ] for all i,j
Return Arr1 [ 1 , 10 , 3 , i , j ]

当然,我需要一流的解决方案(而不是循环为他们一...)

Of course I need the elegant solution (not the "loop for them" one...)

请注意:
我并不想通过数组循环,因为它是11维!!的code将是丑陋且容易出错。,我可能会用完变量名:)

Please Note:
I do not want to loop through the arrays, because it is 11 dimensional!!.. the code will be ugly and error prone.. and I may run out of variable names :)

编辑:
正常的解决办法是:


The normal solution would be:

for(int i=0;i<10;i++)
    for(int j=0;j<10;j++)
       if(Arr2[1,10,3,maxi,maxj]<Arr2[1,10,3,i,j])
       {
         maxi=i
         maxj=j
       }
 return Arr1[1,10,3,maxi,maxj];

不过,我需要做的是在更短的,更美好code ..可使用querys或LINQ。

But I need to do it in less and more beautiful code.. may be using querys or linq..

推荐答案

假设你必须有一个11维数组(做一些M理论工作的?),并没有其他的数据结构是可能的,因为你不想使用循环查找数组中的最大值,留下你只有一个可能性,我能想到的:

Assuming that you have to have an 11-dimensional array (doing some M-theory work?) and that no other data structure is possible, and given that you don't want to use looping to find the maximum value in the array, that leaves you with just one possibility that I can think of:

创建了两个私人领域,一个是价值观,一个用于最大值一个新的ManagedArray类。
创建11个指标参数的索引。该得到的是简单,只要有11个参数,什么都可以:

Create a new ManagedArray class with two private fields, one for values and one for maximums.
Create an indexer with 11 index parameters. The get is as simple as anything with 11 parameters can be:

        public int this[int index1, int index2, int index3, int index4, int index5, int index6, int index7, int index8, int index9, int index10, int index11]
        {
            get
            {
                return (int) values.GetValue( index1, index2, index3, index4, index5, index6, index7, index8, index9, index10, index11 );
            }

本集将调用的SetValue,然后更新当前的最大值与如果新的值大于现有最大的那一套尺寸的新值。
创建一个最大属性需要一串索引值,使其返回那些指标值的最大值的电流值。 现在改变ARR1和ARR2是ManagedArray类的实例。

The set will call SetValue and then update the current maximums with the new value if the new value is bigger than the existing maximum for that set of dimensions.
Create a Maximum property that takes a bunch of index values, have it return the current value of maximums at those index values. Now change Arr1 and Arr2 to be instances of the ManagedArray class.

我们所做的是在请求时有一个稍微费时阵列制定者,跟踪最大值,所以我们可以随时要求看看他们在固定时间内进行交易的循环(如果你想知道的最大值)。

What we've done is to trade a loop at request time (when you want to know the maximum) with a slightly more time-consuming array setter that tracks maximum values so we can look them up in constant time whenever requested.

AFAIK,这是你能做到这一点的唯一方法。

AFAIK, that's the only way you can do it.

请注意,您可以让人们更方便使用 PARAMS 关键字来阅读,但随后的制定者会稍微复杂一点,当你更新的最大值。给你。

Note that you can make it a lot easier to read by using the params keyword, but then the setter will be a little more complicated when you're updating the maximums. Up to you.

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

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