输入一句句子,把句子中的单词放在wordsArray这个数组里面,然后继续输入句子,wordsArray数组再添加新的单词,但是怎样实现添加的单词不重复?举个例子:第一次输入:this is an apple(这时wordsArray数组里应该是{this,is,an,apple})
第二次输入:he is pig(这时wordsArray数组里应该是{this,is,an,apple,he,pig})最好能给一点核心代码,谢谢

解决方案 »

  1.   

    ArrayList wordsArray = new ArrayList();
    string str1 = "this is an apple";
    string str2 = "he is pig";
    string[] s1 = str1.Split(' ');
    for(int i = 0;i < s1.Length;i++)
     {
        if(!wordsArray.Contains(s1[i]))
          {
    wordsArray.Add(s1[i]);
          }
      }
    string[] s2 = str2.Split(' ');
    for(int j = 0;j < s2.Length;j++)
     {
        if(!wordsArray.Contains(s2[j]))
          {
             wordsArray.Add(s2[j]);
          }
     }
      

  2.   

    上面的效率不敢恭维。
    应该先存到一个datatable后进行排序,然后比较上个输入,如果和上次的输入一致则不存,不一致则存
      

  3.   

    可以的话,牺牲空间用HashTable
      

  4.   

    如果你用的是2005,可以使用
                System.Collections.Generic.SortedDictionary<string>
    或者        System.Collections.Generic.Dictionary<string>
    使用起来都很简单也很方便。
      

  5.   

    学习,如果能讲的再详细点就好了。lowtemper(小镇姑娘她爹)
      

  6.   

    直接用ArrayList 啦,要得到数组的话,最后再把ArrayList转换成数组就行了。
      

  7.   

    我只用过ArrayList 不知道和其他的数据结构相比有什么区别?
      

  8.   

    用Hashtable最好,因为有一定的排序算法。
    你的key可以采用一些算法,加快查找速度。对于分析字符串的话,这个的话解决办法就是自己写,split有点慢。网上有这方面的比较。