2015-10-10 40 views
-2

我试图将循环中的值存储在列表中。我的代码是:使用For循环将值存储在列表中

int count = 100; 
for(int i=0;i<=count;i++) 
{ 
    my code to get the text: 
    m.gettextfromelement("xpath"); 
} 

需要的代码存储在一个列表

+0

问题不清楚.... –

+1

errr ....'List.Add'?你是否尝试过研究任何东西? – BradleyDotNET

+0

我想从列表中的列表中保存文本(国家),然后根据字母顺序对其进行排序。 我正在使用for循环来获取这些值,但卡住了如何将该值存储在列表或集合中 – Syed

回答

0
List<string> list = new List<string>(); 
int count = 100; 
for(int i=0;i<=count;i++) 
{ 
    list.Add("StringValue"); 
} 
+1

该代码将创建一个列表,其中100个项目的值都为“StringValue”。 – Jake

+0

“StringValue”是他想要放入列表中的值 – marco

+0

您已经引用了它,所以它会在每次循环时将该特定字符串插入到列表中。 – Jake

0

这是做这件事......值

var stringList = new List<string>(); // The list to store your text 
var count = 100; 
for (var iteration = 0; iteration <= count; iteration++) 
{ 
    //Code to get the text... 
    //Code to get the specific element - var specificElement = m.gettextfromelement("xpath"); 
    //Finally, you "Add()" the specific element text to the list as... 
    stringList.Add(specificElement); 
} 

所有数据结构在C#中使用在MSDN网站上可用。列表的页面是this,FYI。 dotnetpearls是我经常访问的C#的另一个网站。

0
var list = new List<string>(); 
int count = 100; 
for(int i=0;i<=count;i++) 
{ 
    list.Add(m.gettextfromelement("xpath")); 
}