由冒泡排序引出...

  首先,根据冒泡排序写一个C#控制台:

static void Main(string[] args)
{
Console.WriteLine("请输入一串数字:");
string str = Console.ReadLine();
char[] char_str = str.ToCharArray();
for (int i = 0; i < char_str.Length; i++)
{
for (int char_str_long = char_str.Length; char_str_long >1
; char_str_long--)
{
int temp = 0;
if (Convert.ToInt32(char_str[char_str_long - 1]) < Convert.ToInt32(char_str[char_str_long - 2]))
{
temp = char_str[char_str_long - 1];
char_str[char_str_long - 1] = char_str[char_str_long - 2];
char_str[char_str_long - 2] = (char)temp;
}

}
}
Console.WriteLine(char_str);
Console.ReadKey();
}

然后,优化冒泡排序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace maopao
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一串数字:");
string str = Console.ReadLine();
char[] char_str = str.ToCharArray();
for (int i = 0; i < char_str.Length; i++)
{
for (int char_str_long = char_str.Length; char_str_long >1; char_str_long--)
{
int flag = 1;
int temp = 0;
if (Convert.ToInt32(char_str[char_str_long - 1]) < Convert.ToInt32(char_str[char_str_long - 2]))
{
flag = 0;
temp = char_str[char_str_long - 1];
char_str[char_str_long - 1] = char_str[char_str_long - 2];
char_str[char_str_long - 2] = (char)temp;
}
if (flag == 1)
break;
}

}
Console.WriteLine(char_str);
Console.ReadKey();
}
}
}

第三,由上面的2块代码可以知道string------>char[],只需要char[] char_str = str.ToCharArray();这个现成的方法就可以,那么char[]------>string怎么办呢?string strnew String(char_str);为什么要引入这个,由于在后面要引入泛型,涉及到一些。

第四,现在引入泛型。

int[] arrInt= new int[]{7,3,4,5,6};
List<int> cha = new List<int>(arrInt);
for (int i = 0; i < cha.Count - 1; i++)
{
for (int j = cha.Count - 1; j > 0; j--)
{
//int flag = 1;
if (cha[j] < cha[j - 1])
{
//flag = 0;
int temp = cha[j];
cha[j] = cha[j - 1];
cha[j - 1] = (char)temp;
}
//Console.WriteLine(cha);
//Console.ReadKey();
//if (flag ==1)
//break;
}

}
foreach (var item in cha)
{
Console.Write(string.Format("{0} ", item));
}
Console.WriteLine();
Console.ReadKey();
}

上面的东西还不能从页面或者控制台输入一个字符串,下面的主要任务就是把string------>int[]就完事了呗,你说呢。

一种方法是通过循环截取字符串中的数给int[];在一种方法:

int[] arrInt = Array.ConvertAll<string, int>(str.split(','), delegate(string s) { return int.Parse(s); });

int[] arrInt = Array.ConvertAll<string, int>(str.Split(','), s => int.Parse(s));

这种方法可以直接将string------>int[],这是通过泛型方法。但是这种方法实现的是只有在字符串中间有分隔符的情况下,才能够使用。

有没有一种简单的方法,是string------>string[]呢,这个我很不知道有没有简便的方法或者泛型方法?

string[] chaa = new[] {"7","3","4","5","6","2"};
int[] arrInt = Array.ConvertAll<string, int>(chaa, delegate(string s) { return int.Parse(s); });
List<int> cha = new List<int>(arrInt);
for (int i = 0; i < cha.Count - 1; i++)
{
for (int j = cha.Count - 1; j > 0; j--)
{
//int flag = 1;
if (cha[j] < cha[j - 1])
{
//flag = 0;
int temp = cha[j];
cha[j] = cha[j - 1];
cha[j - 1] = (char)temp;
}
//Console.WriteLine(cha);
//Console.ReadKey();
//if (flag ==1)
//break;


}

}
foreach (var item in cha)
{
Console.Write(string.Format("{0} ", item));
}
Console.WriteLine();
Console.ReadKey();
}

希望能找到方法?
1.for (int i = 0; i < s.Length; i++) { ss[i] = s[i].ToString(); }

2.public static int[] ToIntArray(string[] Content)

{int[] c = new int[Content.Length];

for(int i = 0; i < Content.Length; i++) {

c[i] = Convert.ToInt32(Content[i].ToString());

}

 return c;

}
3.http://www.cnblogs.com/wintalen/archive/2010/12/20/1911599.html




posted on 2012-02-28 22:22  daywrite  阅读(155)  评论(0)    收藏  举报

导航