在C#中操作列表 [英] Manipulating lists in C #
问题描述
大家好,
我有一个想法,我仍然不确定如何在实践中解决。
我有一个字符串列表,这些字符串都由6位数组成。 " 012345" ;.我想要的是在列表中添加这些字符串。
现在有了这个列表,我想浏览整个列表并选择每个位置,字符串的第一个数字。最后我想形成一个包含所有数字的数组。
示例:我有一个列表{012345,032165,211458,214563,098796,574623,894235 ,3135698}
滚动列表以形成新的向量,如下所示:[0,0,2,2,0,5,8,3]是列表中每个元素的第一个值。
第一部分我想解决这个问题。
第二部分我将随机选择一个位置,主列表中的任何向量,然后执行以下操作:我想构建一个新列表,只有文件名以数字值绘制矢量开头。
示例:如果向量绘制了值0,我想构建一个只包含这些值的新列表:{012345,032165,098796}
任何想法?如果我对这个想法不是很清楚,请告诉我一些事情
要解决第一个问题,请使用以下代码:
< pre lang =cs> List< string> yourList = new 列表< string>(){ 012345 , 032165, 211458, 214563, 098796, < span class =code-string> 574623, 894235, 3135698};
List< string> firstDigits = yourList.Select(x = > x.Substring( 0 , 1 ))。ToList();
关于第二个问题,我不确定,但试试这个,告诉我是否这不是你想要的:
随机r = new Random();
int max = yourList.Select(x = > x.Length).Min( );
int randomNum = r.Next( 9 );
List< string> newList = yourList.Where(x = > x.StartsWith(randomNum.ToString());
希望这会有所帮助。
第一部分很简单:
List< string> myList = new List< string>(){ 012345, 032165, 211458, 214563, 098796, 574623, 894235, 3135698};
char [] chars = NE w char [myList.Count];
int index = 0 ;
foreach ( string s in myList)
{
chars [index ++] = s.Length > 0 ? s [ 0 ]:' ?' ;
}第二部分也非常简单 - 你所要做的就是为foreach循环添加一个列表和一个简单的条件。
但是因为这听起来像是家庭作业,我会把它留给你!
Hello guys,
I have an idea where I still not sure how to solve in practice.
I have a list of string, these strings are all formed by 6 digits. "012345". what I want is to add these strings in a list.
Now with this list made, I want to go through the whole list and go picking at each position, the first digit of the string. in the end I want to form an array with all digits caught.
example: I have a list {012345, 032165, 211458, 214563, 098796, 574623, 894235, 3135698}
to scroll through the list to form the new vector like this: [0, 0, 2, 2, 0, 5, 8, 3] which is the first value of each element of the list.
first part I want to solve this problem.
second part I will randomly select a position that any vector in the main list and go and do the following: I want to build or a new list with only the file name starts with digit values drawn vector.
example: if the vector drawn the value "0", I want to build a new list with only those values: {012345, 032165, 098796}
any ideas? If I am not very clear on the idea, please tell me something
To solve the first problem, use this code:
List<string> yourList = new List<string>() { "012345", "032165", "211458", "214563", "098796", "574623", "894235", "3135698"}; List<string> firstDigits = yourList.Select(x => x.Substring(0, 1)).ToList();
About the second problem, I'm not sure, but try this, and tell me if this isn't what you want:
Random r = new Random(); int max = yourList.Select(x => x.Length).Min(); int randomNum = r.Next(9); List<string> newList = yourList.Where(x => x.StartsWith(randomNum.ToString());
Hope this helps.
The first part is easy:
List<string> myList = new List<string>() { "012345", "032165", "211458", "214563", "098796", "574623", "894235", "3135698" }; char[] chars = new char[myList.Count]; int index = 0; foreach (string s in myList) { chars[index++] = s.Length > 0 ? s[0] : '?'; }The second part is also pretty simple - all you have to do is add a list, and a simple condition to the foreach loop.
But since this sounds like homework, I'll leave that to you!
这篇关于在C#中操作列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!