[英]Cast From Arraylist To a String
我使用Random Class从ArrayList中获取一个元素。 目前我正在尝试使用ToString将此元素转换为字符串(失败)。 我如何将解决方案[r]转换为字符串?
ArrayList Solutions = new ArrayList(5);
Solutions.Add("The Odyssey");
Solutions.Add("Dune");
Solutions.Add("Sherlock Holmes");
Solutions.Add("Othello");
Solutions.Add("Of Mice and Men");
Random ran = new Random();
int r = ran.Next(Solutions.Count);
string s = Solutions[r].ToString;
string s = Solutions[r].ToString(); // you're missing brackets
使用IList<string>
使它更容易和干净。
IList<string> Solutions = new List<string>(5);
Solutions.Add("The Odyssey");
Solutions.Add("Dune");
Solutions.Add("Sherlock Holmes");
Solutions.Add("Othello");
Solutions.Add("Of Mice and Men");
Random ran = new Random();
int r = ran.Next(Solutions.Count);
string s = Solutions[r];
List<String> solutions = new List<String>();
solutions.Add("The Odyssey");
solutions.Add("Dune");
// and so on
那么你也能:
label1 = solution[random_index]; // starts with 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.