2016-12-28 130 views
-1
public void ImagesLinks() 
{ 
    int count = 0; 
    foreach(string c in DatesAndTimes) 
    { 
     if (count == 9) 
     { 
      count = 0; 
     } 
     string imageUrl = firstUrlPart + countriescodes[count] + secondUrlPart + DatesAndTimes[count] + thirdUrlPart + "true"; 
     imagesUrls.Add(imageUrl); 
     count++; 
    } 
} 

列表DatesAndTimes採用以下格式:每行都是列表中的項目。添加到列表時,如何跳過每個9項目?

201612281150201612281150 
201612281150201612281151 
201612281150201612281152 
201612281150201612281153 
201612281150201612281154 
201612281150201612281155 
201612281150201612281156 
201612281150201612281157 
201612281150201612281158 
201612281150201612281159 
Europe 
201612281150201612281150 
201612281150201612281151 
201612281150201612281152 
201612281150201612281153 
201612281150201612281154 
201612281150201612281155 
201612281150201612281156 
201612281150201612281157 
201612281150201612281158 
201612281150201612281159 
Turkey 

每10個項目都有一個名稱。 我想在構建鏈接時跳過該名稱。 所以我數到9每次(從0到9計數10次)然後我重置計數器爲0,我也想跳過名稱,例如歐洲,然後數到10再跳過土耳其等等。

+4

那豈不是更容易只是過濾掉包含字母串? – RandomStranger

+0

您可以使用'count'來索引兩個不同的數組'countrycodes'和'DatesAndTimes'。看起來,無論你有一個大型陣列,每個國家有9個時間,或者你應該得到IndexOutOfRange異常。你也不會使用'c'。我會完全修改此代碼。 – MotKohn

回答

2

在if語句中使用continue。它將在跳過該行並繼續執行循環執行foreach

public void ImagesLinks() 
      { 
       int count = 0; 
       foreach(string c in DatesAndTimes) 
       { 
        if (count == 9) 
        { 
         count = 0; 
         continue; 
        } 
        string imageUrl = firstUrlPart + countriescodes[count] + secondUrlPart + DatesAndTimes[count] + thirdUrlPart + "true"; 
        imagesUrls.Add(imageUrl); 
        count++; 
       } 
      } 
4

您還可以使用(count % 9 == 0),這樣你就不必重置計數器:

public void ImagesLinks() 
{ 
    int count = 0; 
    foreach(string c in DatesAndTimes) 
    { 
     if (count++ % 9 == 0) 
      continue; 

     string imageUrl = firstUrlPart + countriescodes[count] + secondUrlPart + DatesAndTimes[count] + thirdUrlPart + "true"; 
     imagesUrls.Add(imageUrl); 
    } 
}