0

これまで、私は単純な配列を使用して必要な情報を入力および取得していました。

最初の例は次のとおりです。

            // ===Example 1. Enter info ////
            string[] testArr1 = null;
            testArr1[0] = "ab";
            testArr1[1] = "2";
            // ===Example 1. GET info ////
            string teeext = testArr1[0];
            // =======================

そして2番目の例:

            // ====== Example 2. Enter info ////
            string[][] testArr2 = null;
            List<int> listTest = new List<int>();
            listTest.Add(1);
            listTest.Add(3);
            listTest.Add(7);
            foreach (int listitem in listTest)
            {
                testArr2[listitem][0] = "yohoho";
            }
            // ====== Example 2. Get info ////
            string teeext2 = testArr2[0][0];
            // =======================

しかし、現在、各配列に識別番号を割り当てようとしているので、1つのConcurrentDictionaryで複数の異なる配列を識別できます。

辞書の配列から情報を入力および取得するにはどうすればよいですか?

ほら、2つの識別子と2つの辞書があります。

            decimal identifier1 = 254;
            decimal identifier2 = 110;
            ConcurrentDictionary<decimal, string[]> test1 = new ConcurrentDictionary<decimal, string[]>();
            ConcurrentDictionary<decimal, string[][]> test2 = new ConcurrentDictionary<decimal, string[][]>();

私はこのようなものを想像していました:

//////////Example 1
//To write info
test1.TryAdd(identifier1)[0] = "a";
test1.TryAdd(identifier1)[1] = "b11";

test1.TryAdd(identifier2)[0] = "152";
//to get info
string value1 = test1.TryGetValue(identifier1)[0];
string value1 = test1.TryGetValue(identifier2)[0];

//////////Example 2
//To write info: no idea
//to get info: no idea

PS:上記のコードは機能しません(自作であるため)。では、ConcurrentDictionaryのstring[]とstring[] []にIDで情報を入力する正しい方法は何ですか?(そしてそれを取り出すために)

4

1 に答える 1

0

あなたの宣言を考えると

decimal identifier1 = 254;
decimal identifier2 = 110;
ConcurrentDictionary<decimal, string[]> test1 = new ConcurrentDictionary<decimal, string[]>();
ConcurrentDictionary<decimal, string[][]> test2 = new ConcurrentDictionary<decimal, string[][]>();

このようなコードを使用できます

  test1[identifier1] = new string[123];
  test1[identifier1][12] = "hello";

  test2[identifier2] = new string[10][];
  test2[identifier2][0] = new string[20];
  test2[identifier2][0][1] = "world";

データを入力します。入力したデータにアクセスする例を次に示します。

  Console.WriteLine(test1[identifier1][12] + " " + test2[identifier2][0][1]);

ただし、このようなコードは、特にtest2の場合、非常に複雑でデバッグが難しい傾向があることを言わなければなりません。ジャグ配列を使用してもよろしいですか?

于 2012-03-26T20:37:24.033 に答える