最近做了一个关于去除字符串数组中重复元素的程序
结果如下
using System;
class test
{
static void Main()
{
int j,k=0,flag;
string[]str=new string[]{"aaa","bbb","ccc","aaa","ccc"};
string[]a=new string[5];       //定义新的数组
a[0]=str[0];
for(int i=1;i<5;i++)
{
flag=1;
j=0;
while(j<=k)             // 循环!有相同元素就标志为0
if(a[j]==str[i])
flag=0;
if(flag==1)             //标志不为0则赋值给新的数组元素
{
k++;
a[k]=str[i];
}
}
Console.WriteLine("新的道德数组是:");         //输出新数组元素
for(j=0;j<=k;j++)
Console.Write("{0} ",a[k]);
}
}
但编译的时候只是出现DOC介面!什么也不显示!而且电脑CPU使用率达到100%!是在逻辑上有什么问题吗?求各位高手指点一二

解决方案 »

  1.   

    using System;namespace ConsoleApplication1
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class Class1
    {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    string[] str = new string[]{"aaa","bbb","ccc","aaa","ccc"}; for (int i = 0; i < str.Length; i++)
    for (int j = i + 1; j < str.Length; j++)
    {
    if (str[i] != null && str[i] == str[j]) str[i] = null;
    } foreach (string s in str) Console.WriteLine(s); Console.Read();
    }
    }
    }
      

  2.   

    跟踪调试一下就知道
    while(j<=k)             // 循环!有相同元素就标志为0
    if(a[j]==str[i])
    flag=0;
    其中j=0,k=0 ;那不就总是处在循环中,
      

  3.   

    我现在把程序改成了这样!
    using System;
    class test
    {
    static void Main()
    {
    int j,k=0,flag;
    string[]str=new string[]{"aaa","bbb","ccc","aaa","ccc"};
    string[]a=new string[5];       //定义新的数组
    a[0]=str[0];
    for(int i=1;i<5;i++)
    {
    flag=1;
    j=0;
    while(j<=k) 
    {
    if(a[j]==str[i])         // 循环!有相同元素就标志为0
    {
    flag=0;
    break;
    }
    j++;
    }
    if(flag==1)             //标志不为0则赋值给新的数组元素
    {
    k++;
    a[k]=str[i];
    }
    }
    Console.WriteLine("新的道德数组是:");         //输出新数组元素
    for(j=0;j<=k;j++)
    Console.Write("{0} ",a[k]);
    }
    }
    但他运行后结果却是这样!
    ccc ccc ccc 
    这是怎么回事啊?