2016-12-28 131 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); 
    } 
}