0

このような多次元配列を使用してリストを作成しようとしています。

[validatorKey][counter]
1453          10
1231          12
6431          7
1246          1
1458          2

しかし、私はそれに対処できませんでした。ちなみに私の方法です。また、メソッドの最後で配列サイズをインクリメントする必要があります。Array.Resize(ref array, 2); を使用する必要があることはわかっています。しかし、私の配列は多次元であるため、この場合、適切な方法は何でしょうか.

private int AracaAitSeferSayisiDondur(int pValidatorKey)
{
     int iSeferSayisi = 0;
     int[,] iSeferListesi = (int[,])ViewState["SeferListesi"];
     if (iSeferListesi == null)
     iSeferListesi = new int[1,1];

     bool aynisiVarmi = false;

     for (int i = 0; i < iSeferListesi.Length; i++)
     {
        if (iSeferListesi[i,0] == pValidatorKey)
        {
           aynisiVarmi = true;
           iSeferListesi[i,1]++;
           iSeferSayisi = iSeferListesi[i,1]++;
           break;
        }
     }
     if (!aynisiVarmi)
     {
        int arrayLength = iSeferListesi.Length;
        iSeferListesi[arrayLength--, 0] = pValidatorKey;
        iSeferListesi[arrayLength--, 1] = 1;
        //IN THIS PART ARRAY SIZE SHOULD BE INCREASED
        iSeferSayisi = iSeferListesi[arrayLength--, 1];
     }
     ViewState["SeferListesi"] = iSeferListesi;
     return iSeferSayisi;
}
4

2 に答える 2

1

プロパティは、配列内の要素のLength総数を返します。

GetLength(dimension)メソッドを使用して次元のサイズを取得します。

for (int i = 0; i < iSeferListesi.GetLength(0); i++)

と:

int arrayLength = iSeferListesi.GetLength(0);
于 2012-09-30T12:22:04.663 に答える
1

次のようなものが必要だと思います:

// not tested
private int AracaAitSeferSayisiDondur(int pValidatorKey)
{
    var iSeferListesi = (Dictionary<int,int>)ViewState["SeferListesi"];
     if (iSeferListesi == null)
        iSeferListesi = new Dictionary<int,int>;

     int iSeferSayisi;

    if ( iSeferListesi.TryGetValue(pValidatorKey, out iSeferSayisi)
    {
       iSeferSayisi += 1;
       iSeferListesi[pValidatorKey] = iSeferSayisi;
       iSeferSayisi += 1;  // is this OK ??
    }
    else
    {
       iSeferSayisi = 1;
       iSeferListesi[pValidatorKey] = iSeferSayisi;
    }

    ViewState["SeferListesi"] = iSeferListesi;
    return iSeferSayisi;
}

iSeferListesi (コードから派生) の 2 倍のインクリメントは、おそらく必要なものではありません。これがないと、if/else ロジックがさらに単純になります。

于 2012-09-30T12:47:51.830 に答える