using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TListExample
{
class Program
{
static void Main(string[] args)
{
//1.合并两个集合,去除重复
List<string> list1 = new List<string>() { "a", "b", "c", "d", "e" };
List<string> list2 = new List<string>() { "f", "g", "h", "b", "e" };
foreach (var l2 in list2)
{
if (!list1.Contains(l2))
{
list1.Add(l2);
}
}
foreach (var l1 in list1)
{
Console.WriteLine(l1);
}
Console.WriteLine("-------------------------------");
//2.随机生成10个1~100之间的不重复的偶数,添加到List中
Random r = new Random();
List<int> list3 = new List<int>();
for (int i = 0; i < 10; i++)
{
int rNumber = r.Next(1, 101);
if (!list3.Contains(rNumber)&&rNumber%2==0)
{
list3.Add(rNumber);
}
else
{
i--;
}
}
foreach (var item in list3)
{
Console.WriteLine(item);
}
Console.WriteLine("---------------------------");
//3.分拣奇偶数程序用泛型实现
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List<int> listJi = new List<int>();
List<int> listOu = new List<int>();
for (int i = 0; i < nums.Length; i++)
{
if (nums[i]%2==0)
{
listOu.Add(nums[i]);
}
else
{
listJi.Add(nums[i]);
}
}
listJi.AddRange(listOu);
foreach (var item in listJi)
{
Console.WriteLine(item);
}
Console.WriteLine("---------------------------");
//4.大小写转换
string str = "0零 1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖";
Dictionary<char, char> dic = new Dictionary<char, char>();
string[] strNew = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < strNew.Length; i++)
{
dic.Add(strNew[i][0], strNew[i][1]);
}
Console.WriteLine("请输入小写,我们将转换成大写");
string input = Console.ReadLine();
for (int i = 0; i < input.Length; i++)
{
if (dic.ContainsKey(input[i]))
{
Console.Write(dic[input[i]]);
}
else
{
Console.Write(input[i]);
}
}
Console.ReadKey();
}
}
}
网友评论