2017-02-11 14 views
0

私は は私が実証するための簡単なコードを書いたい順序で値を取得中に問題があります。配列リスト内の2つの文字列リストの最初、2番目...値を取得する方法は?

List<string> st1 = new List<string>() {"st11","st12"}; 
List<string> st2 = new List<string>() {"st21","st22"}; 
ArrayList stringArrayList = new ArrayList(); 
stringArrayList.Add(st1); 
stringArrayList.Add(st2); 

string[] n1 = new string[10]; 
int i = 0; 
foreach (List<string> item in stringArrayList) 
{ 
    foreach (var item2 in item) 
    { 
     n1[i] = item2; 
     i++; 
    } 
} 

このコードで出力は次のようになります。ST11、ST12のST21、S22

私が欲しいのリストのな長さが同じであればあなたができるST11、ST21のST12、ST22

私は情報をn1

+0

リストの長さが異なる場合はどうしますか? – NineBerry

+0

私の場合は長さが違う –

答えて

3

にこの順「ST11、ST21のST12、ST22」に格納したい:それはこのような値を取得しますsometを作る

長さが等しくない場合は、最小値を計算できます。それぞれの要素の最小長をコピーし、残りをコピーします。

1

まず、最大リストの長さを確認してから、すべてのリストからインデックス(0,1,3 ...から最大)までアイテムを取得します。インデックスが存在するかどうかを確認することを忘れないでください。さらに、正確なサイズをn1に設定できるのは、すべてのリスト数の合計であるためです。この場合、i++の区切り線は必要ありません。

 List<string> st1 = new List<string> { "st11", "st12" }; 
     List<string> st2 = new List<string> { "st21", "st22" }; 
     List<List<string>> stringArrayList = new List<List<string>> {st1, st2}; 
     int maxCount = stringArrayList.Max(x => x.Count); 
     int totalItems = 0; 
     stringArrayList.ForEach(x=> totalItems+= x.Count); 
     string[] n1 = new string[totalItems]; 
     int i = 0; 

     for (int index = 0; index < maxCount; index++) 
     { 
      foreach (var list in stringArrayList) 
      { 
       if (list.Count > index) 
       { 
        n1[i++] = list[index];      
       } 
      } 
     } 
1

2つのループを逆にして外側ループを内側ループにする必要があります。

文字列配列の長さを区切り文字として使用して、外側ループのforeachループの代わりにforループを使用します。

また、ArrayListは使用しないで、実際の型付きリストを使用してください。

List<string> st1 = new List<string>() { "st11", "st12" }; 
List<string> st2 = new List<string>() { "st21", "st22" }; 

List<List<string>> stringArrayList = new List<List<string>>(); 
stringArrayList.Add(st1); 
stringArrayList.Add(st2); 

// Get length of first string array 
int firstArrayLength = stringArrayList[0].Count; 

string[] n1 = new string[10]; 
int i = 0; 

// For each position in the arrays from 0 to firstArrayLength -1 do 
for (int arrayPosition = 0; arrayPosition < firstArrayLength; arrayPosition++) 
{ 
    // For each of the string array 
    foreach (var stringArray in stringArrayList) 
    { 
     // Get the item from the stringArray at position arrayPosition 
     n1[i] = stringArray[arrayPosition]; 
     i++; 
    } 
} 
2

ここでは、探していることを行う実装があり、ギザギザの配列も処理されます。

List<string> st1 = new List<string>() { "st11", "st12" }; 
List<string> st2 = new List<string>() { "st21", "st22", }; 
ArrayList stringArrayList = new ArrayList(); 
stringArrayList.Add(st1); 
stringArrayList.Add(st2); 

//this will protect us against differing max indexes if the 2D array is jagged. 
int maxIndex = 0; 
int totalElements = 0; 
foreach (List<string> currentList in stringArrayList) 
{ 
    if (currentList.Count > maxIndex) 
    { 
     maxIndex = currentList.Count; 
    } 
    totalElements += currentList.Count; 
} 

string[] n1 = new string[totalElements]; 
int i = 0; 
for (int j = 0; j < maxIndex; j++) 
{ 
    for (int k = 0; k < stringArrayList.Count; k++) 
    { 
     List<string> currentStringArray = (List<string>)stringArrayList[k]; 
     if (j < currentStringArray.Count) 
     { 
      n1[i] = currentStringArray[j]; 
      i++; 
     } 

    } 
} 
関連する問題