In Java, I can assign a value from Vector to a String variable.
String str = vector1.elementAt(0).toString();
How can I do the same thing in C# with List?
Thanks.
List<string> list = ...
...
string str = list[0];
...
You can use index with the list.
List<string> list = new List<string>();
string str = list[0];
There are many ways to do this:
Assuming
List<string> yourList;
Then all of the following would put the element in position index
inside a string variable:
string s = yourList[index];
string s = yourList.ToArray()[index];
string s = yourList.ElementAt(index);
In all of the above, index
must fall within the range 0 - (yourList.Length-1)
since array indexing in C# is zero-based.
This, on the other hand, while it would seem the same, won't even compile:
string s = youList.Skip(index).Take(1);
.Take()
in this case doesn't return a string
but a IEnumerable<string>
which is still a collection.
String str = vector1[0].ToString();
//Creating a list of strings
List<string> lst = new List<string>();
...
//The string is filled with values, i is an int
string ithValue = lst[i];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.