ArrayList包含问题 [英] ArrayList contains problem

查看:74
本文介绍了ArrayList包含问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在添加列表后用2路StreamReader和File.ReadAllText读取了一个txt



I read a txt with 2 way StreamReader and File.ReadAllText after add the list

foreach (string line in File.ReadAllText(@openFileDialog1.FileName).Split(','))
               {
                   if (!ary.Contains(line.ToLower()) && line.Length > 2)
                   {
                       ary.Add(line.ToLower());
                   }
               }





但包含不工作添加相同的字符串

因为字符串相似但不同代码如何修复它。

那么如何检查包含哈希码

我尝试列表,dicitonary但不改变任何东西



but contains dont work add same strings
because the strings are similar but diffrent has code how can I fix it.
So how can I check contains with hashcode
I try list,dicitonary but dont change anything

TextReader txt = File.OpenText(openFileDialog1.FileName);



是工作但是我不拆分它很容易,因为我的txt文件是英文书籍:)


is work but I dont split it with easily ,because my txt file a english book :)

推荐答案

问题和解决方案中的代码都是错误的,即使你认为它有效 。如果某些东西有效,但你不明白,它不起作用。



首先,永远不要使用类 ArrayList 早在.NET Framework v.2.0中,当引入泛型时,它已经过时了。它没有正式停放 [已淘汰] 只是因为通过一些遗留代码使用它没有任何问题。对于新开发,您需要使用类 System.Collections.Generics.List<> 。它不需要潜在的不安全元素类型转换。



现在,你真正需要获得正确分割线的是类系统。 IO.File.ReadAllLines http://msdn.microsoft .com / zh-cn / library / system.io.file.aspx [ ^ ]。



不仅你的代码是冗余的,它也是不可移植的。请参阅: http://en.wikipedia.org/wiki/End-of-line [< a href =http://en.wikipedia.org/wiki/End-of-linetarget =_ blanktitle =New Window> ^ ]。



-SA
Both the code in the question and "solution" are wrong, even if you think it "works". If something works but you don''t understand how, it does not work.

First of all, never use the class ArrayList it was rendered obsolete as early as of .NET Framework v.2.0, when generics were introduced. It wasn''t formally parked [Obsolete] just because there is nothing wrong in using it through some legacy code. For new development, you need to use the class System.Collections.Generics.List<>. It won''t require potentially unsafe element type casting.

Now, what you really need to obtain correctly split lines is the class System.IO.File.ReadAllLines: http://msdn.microsoft.com/en-us/library/system.io.file.aspx[^].

Not only your code is redundant, it''s also non-portable. Please see: http://en.wikipedia.org/wiki/End-of-line[^].

—SA


解决了回车拆分(''\ r'')为什么?我不知道,但它的工作;)
Solved with "carriage return" split(''\r'') why? I dont know but its work ;)


这篇关于ArrayList包含问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆