两个arraylists之间的差异除外 [英] Diff between two arraylists can't read except

查看:65
本文介绍了两个arraylists之间的差异除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   static   void  diff(ArrayList args)
{
if (args.Count < ; 2 || args.Count > 3
{
Console.WriteLine( Insflaicent of Args );
}
其他 如果(args.Count == 3
{
string source_path = get_abs_path(args [ 1 ]的ToString());
StreamReader sr = new StreamReader(source_path);
ArrayList text1 = new ArrayList();
string [] file = sr.ReadToEnd()。Split(' \\\
');
foreach string line in file)
{
text1.Add(line);
}
sr.Close();
Console.WriteLine( + source_path + );
Console.WriteLine(file);

source_path = get_abs_path(args [ 2 ]。ToString());
sr = new StreamReader(source_path);
ArrayList text2 = new ArrayList();
string [] file2 = sr.ReadToEnd()。Split(' \\\
');
foreach string lin in file2)
{
text2.Add(lin);
}
sr.Close();
ArrayList difference = new ArrayList(text1.Except(text2)); // 这里有什么问题!

}
}

解决方案

消息准确无误:

' System.Collections.ArrayList'不包含'Except'的定义,并且没有扩展方法'Except'可以找到接受类型'System.Collections.ArrayList'的第一个参数(你是否缺少using指令或汇编引用? )



但没有那么有帮助。



基本上,你不应该使用ArrayList - 它不属于date,近十年前被Generic集合类型取代了!



ArrayLists不支持IEnumerable< T>接口,允许他们使用Linq扩展方法。将ListList替换为List< string>相反,它应该工作正常。





------- OFF主题答案------- br />


你在解决方案中写的东西让我感到困惑:ArrayLists不支持IEnumerable接口,它允许他们使用Linq扩展方法。

我认为它应该是:ArrayLists不支持IEnumerable接口,这将允许他们使用Linq扩展方法。或者ArrayLists不支持IEnumerable接口,这会阻止你使用Linq扩展方法。

干杯!






啊!这是可以理解的,但是......

这里涉及两个接口:IEnumerable(旧的,允许你使用foreach循环)和IEnumerable< T>在.NET V2.0中引入的较新的强类型泛型版本



前者不一样(并且IEnumerable< T>派生自IEnumerable)并且只有后者将使用Linq方法,因为它们都是以IEnumerable< T>为基础的。 - 如果你看一下链接方法的MSDN描述: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except(v = vs.110).aspx [ ^ ]它是一个IEnumerable< T>方法,而不是IEnumerable。



试试吧:

  public   class  enumableObject:IEnumerable 
{
public IEnumerator GetEnumerator( )
{
throw new NotImplementedException();
}
}
public class enumerableGeneric< T> :IEnumerable< T>
{
public IEnumerator< T> GetEnumerator()
{
throw new NotImplementedException();
}

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}

然后

列表< string> list =  new  List< string>(); 
var x = list.Except(list);

工作正常

 enumerableGeneric< string>例如=  new  enumerableGeneric< string>(); 
var z = eg.Except(eg);

工作正常

 enumableObject eo =  new  enumableObject() ; 
var y = eo.Except(eo);

给出编译错误:

 MyNamespace.frmMain.enumableObject'不包含'Except'的定义且没有扩展名方法'除了'接受类型'MyNamespace.frmMain.enumableObject'的第一个参数可以找到(你是否缺少using指令或汇编引用?)

与ArrayList完全相同。


方法返回 IEnumerable ,您需要将结果转换为列表:



 list1 = list1.Except(list2).ToList(); 





将ArrayList转换为List:



使用System; 
使用System.Collections;
使用System.Collections.Generic;

静态类扩展
{
/// < 摘要 >
///将ArrayList转换为List。
/// < / summary > ;
公共静态列表< T > ToList < T > (此ArrayList arrayList)
{
List < T > list = new List < T > (arrayList.Count);
foreach(arrayList中的T实例)
{
list.Add(instance);
}
返回清单;
}
}

class程序
{
static void Main()
{
//创建ArrayList。
ArrayList arrayList = new ArrayList();
arrayList.Add(1);
arrayList.Add(2);
arrayList.Add(3);

//使用扩展方法。
列表< int > list = arrayList.ToList < int > ();
foreach(列表中的int值)
{
Console.WriteLine(value);
}
}
}





输出:



1

2

3





我希望这会对你有所帮助。


public static void diff(ArrayList args)
      {
          if (args.Count < 2 || args.Count > 3)
          {
              Console.WriteLine("Insufficent Number of Args");
          }
          else if (args.Count == 3)
          {
              string source_path = get_abs_path(args[1].ToString());
              StreamReader sr = new StreamReader(source_path);
              ArrayList text1 = new ArrayList();
              string [] file = sr.ReadToEnd().Split('\n');
              foreach (string line in file)
              {
                  text1.Add(line);
              }
              sr.Close();
              Console.WriteLine("Content of " + source_path + " :");
              Console.WriteLine(file);

              source_path = get_abs_path(args[2].ToString());
              sr = new StreamReader(source_path);
              ArrayList text2 = new ArrayList();
              string[] file2 = sr.ReadToEnd().Split('\n');
              foreach (string lin in file2)
              {
                  text2.Add(lin);
              }
              sr.Close();
              ArrayList difference = new ArrayList(text1.Except(text2)); // what is the problem here !

          }
      }

解决方案

The message is accurate:

'System.Collections.ArrayList' does not contain a definition for 'Except' and no extension method 'Except' accepting a first argument of type 'System.Collections.ArrayList' could be found (are you missing a using directive or an assembly reference?)


But not that helpful.

Basically, you shouldn't be using ArrayList - it's out of date, and was replaced by the Generic collection types nearly ten years ago!

ArrayLists do not support the IEnumerable<T> interface, which allows them to use Linq extension methods. Replace your ArrayList's with List<string> instead and it should work fine.


-------OFF TOPIC ANSWER SECTION-------

"There is something confusing me in what you wrote in your solution: "ArrayLists do not support the IEnumerable interface, which allows them to use Linq extension methods."
I think it should be: "ArrayLists do not support the IEnumerable interface, which would allow them to use Linq extension methods." or maybe "ArrayLists do not support the IEnumerable interface, which prevents you from using Linq extension methods on them."
Cheers!"



Ah! It's understandable, but...
There are two Interfaces involved here: IEnumerable (the old one, that allows you to use foreach loops) and IEnumerable<T> the newer, strongly typed generic version introduced at .NET V2.0

The former is not the same (and IEnumerable<T> derives from IEnumerable) and only the latter will work with Linq methods, because they are all predicated upon IEnumerable<T> - if you look at the MSDN description for a link method: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except(v=vs.110).aspx[^] it is an IEnumerable<T> method, not IEnumerable.

Try it:

public class enumableObject : IEnumerable
    {
    public IEnumerator GetEnumerator()
        {
        throw new NotImplementedException();
        }
    }
public class enumerableGeneric<T> : IEnumerable<T>
    {
    public IEnumerator<T> GetEnumerator()
        {
        throw new NotImplementedException();
        }

    IEnumerator IEnumerable.GetEnumerator()
        {
        throw new NotImplementedException();
        }
    }

Then

List<string> list = new List<string>();
var x = list.Except(list);

Works fine

enumerableGeneric<string> eg = new enumerableGeneric<string>();
var z = eg.Except(eg);

Works fine

enumableObject eo = new enumableObject();
var y = eo.Except(eo);

Gives a compilation error:

MyNamespace.frmMain.enumableObject' does not contain a definition for 'Except' and no extension method 'Except' accepting a first argument of type 'MyNamespace.frmMain.enumableObject' could be found (are you missing a using directive or an assembly reference?)	

Exactly the same as a ArrayList.


The Except method returns IEnumerable, you need to convert the result to list:

list1 = list1.Except(list2).ToList();



Convert ArrayList to List:

using System;
using System.Collections;
using System.Collections.Generic;

static class Extensions
{
    /// <summary>
    /// Convert ArrayList to List.
    /// </summary>
    public static List<T> ToList<T>(this ArrayList arrayList)
    {
    List<T> list = new List<T>(arrayList.Count);
    foreach (T instance in arrayList)
    {
        list.Add(instance);
    }
    return list;
    }
}

class Program
{
    static void Main()
    {
    // Create ArrayList.
    ArrayList arrayList = new ArrayList();
    arrayList.Add(1);
    arrayList.Add(2);
    arrayList.Add(3);

    // Use extension method.
    List<int> list = arrayList.ToList<int>();
    foreach (int value in list)
    {
        Console.WriteLine(value);
    }
    }
}



Output:

1
2
3


I hope this will help to you.


这篇关于两个arraylists之间的差异除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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