1: Put an odd number in one array into a collection, then put an even number in another collection, and eventually merge the two collections into a single collection, and the odd numbers appear on the left and the even to the right.
Ideas:
(1) Determining the parity of elements in an array, assigning values to 2 sets, respectively
(2) Declaring the third set, passing in the odd and even 2 sets separately | | Add another collection directly with one of the collections
Question: How to Output: "Odd: 1,3,5,7 | Even: 2,4,6,8 "?
int[] Nums = {1,2,3,4,5,6,7,8,9 }; List<int> Jishu =Newlist<int>(); List<int> Oushu =Newlist<int>(); //list<int> all = new list<int> (); for(inti =0; I < Nums. Length-1; i++) { if(Nums[i]%2!=0)//Odd{jishu.add (nums[i]); } Else//even{oushu.add (nums[i]); }} jishu.addrange (Oushu); foreach(varIteminchJishu) {Console.Write (item+" "); } console.readkey ();
2: Prompts the user to enter a string to assign the user-entered string to a character array through a foreach loop
Ideas:
Receives the user input directly with a string, and then assigns each element in the string to a character array through a foreach
Console.WriteLine ("Please feel free to enter"); stringstr =Console.ReadLine (); Char[] CHS =New Char[Str. Length]; inti =0; foreach(varIteminchstr) {Chs[i]=item; I++; } foreach(varIteminchCHS) {Console.Write (item+" "); } console.readkey ();
3: Count the number of occurrences of each character in welcome to, regardless of case
Ideas:
Number of characters---> occurrences
Key-----> value
Put each character in the string as a key into the key value pair set, and put the number of occurrences of each character in the value of the key value pair.
stringstr ="Welcome to China"; Dictionary<Char,int> dic =Newdictionary<Char,int>(); for(inti =0; I < Str. Length; i++) { if(Str[i] = =' ') { Continue; } //if DIC already contains the key that is currently circulating, the value of this key is +1 if(DIC. ContainsKey (Str[i])) {//value +1dic[str[i]]++; } Else//this character appears in the collection for the first time.{Dic[str[i]]=1; } } foreach(keyvaluepair<Char,int> KVinchdic) {Console.WriteLine ("the letter {0} appeared {1} times", KV. Key, KV. Value); } console.readkey ();
. NET Learning Notes----2015-06-25 (Practice for generic collections)