2

我正在尝试使用二维整数数组,但我遇到了一些问题。我想做的是这样的:

int[,] values = new int[Apples,1]; //Apples = say, 50

我想要结束的是这样的:

values={ {393,0},{120,1},{9133,2},{75,3},...};393,120等是在for循环中生成的值。也就是说,我不能通过分配它来初始化数组{ {xx},{yy}},等等。所以我想做一些类似的事情

for (int i = 0; i<Oranges; i++) {

 values[i,0]={functionCall(),i};
}

其中 functionCall 的原型类似于 int functionCall(){...}

但那个任务values[i,0]不起作用。分配过程完成后,我需要按第一列对数组进行排序,所以我会得到一个新数组valuesSorted,如下所示:

valuesSorted={ {75,3},{120,1},{393,0},{9133,2},...} 

因此,当我迭代时,valuesSorted我会按该顺序获取数据。

有什么想法我会怎么做?

4

2 回答 2

4

First off let's address your specific problems. The array size should be two, not one; a multidimensional array with one dimension sized to one doesn't make much sense.

int[,] values = new int[50,2]; 

Next, how do you initialize this array in a loop?

for (int i = 0; i<Oranges; i++)
  values[i,0]={functionCall(),i};

This is illegal because the { } syntax can only be used in an array initializer. Instead you mean to say:

for (int i = 0; i < 50; i++)
{
  values[i,0]= functionCall();
  values[i,1]= i;
}

Next, how do you sort the array?

You don't. Multidimensional arrays are hard to sort and there are few tools for doing so. You'd have to write your own sort algorithm to do it in place.

Since that is the goal and we are now frustrated, we should take a step back and reconsider whether a 2-d array is the correct data structure. It isn't. Here's the right way to solve your problem:

struct Pair<T>
{
  public T First { get; private set; }
  public T Second { get; private set; }
  public Pair(T first, T second) : this()
  {
    First = first;
    Second = second;
  }
}
...
static IEnumerable<Pair<int>> Pairs()
{
  int i = 0;
  while(true)
  {
    yield return new Pair<int>(functionCall(), i);
    i = i + 1;
  }
}
...
List<Pair<int>> pairs = 
  Pairs()
    .Take(50)
    .OrderBy(p=>p.First)
    .ToList();

And now we have a sorted list of fifty pairs of ints, which is what you wanted. Moreover, it is extremely clear from the code that we have a sorted list of fifty pairs of ints.

Here is an alternative solution which uses the "select with index" feature that I like a bit better:

static IEnumerable<int> Values()
{
  while(true)
    yield return functionCall();
}
...
List<Pair<int>> pairs = 
  Values()
    .Take(50)
    .Select((item, index)=>new Pair<int>(item, index))
    .OrderBy(p=>p.First)
    .ToList();

When you use data structures that represent the shape of your data accurately and use sequence operators on sequences, life gets easier.

于 2013-10-02T15:31:45.513 回答
0

我认为您正试图将原始顺序保留在第 2 列中,但也可以使用 aDictionary<int, int>SortedList<int,int>

尝试这个:

class Program
{
    static Random rnd=new Random();

    static int functionCall()
    {
        return rnd.Next(1, 1000);
    }
    static void Main(string[] args)
    {
        var original=new Dictionary<int,int>(10);
        for(int i=0; i<10; i++)
        {
            original.Add(functionCall(), i);
        }
        // original:
        //
        // [646, 0]
        // [130, 1]
        // [622, 2]
        // [454, 3]
        // ...
        // [658, 9]

        var sorted=new SortedList<int, int>(original);

        // sorted:
        //
        // [ 90, 5]
        // [130, 1]
        // [404, 7]
        // [454, 3]
        // ...
        // [756, 8]
    }
}

请注意,Random()已添加到模拟您的functionCall().

于 2013-10-02T18:15:57.943 回答