在 C# 中使用 weka:无法将“java.util.ArrayList"类型的对象转换为“System.Collections.Generic.List"
我目前正在做一个项目,我需要在其中使用 FP-Growth一>算法.我知道 Weka 是一个方便的工具.但是,我使用 C# 进行编码(由于我需要其他一些库).因此,我使用 IKVM.NET
将 weka.jar
转换为 weka.dll
.下面是我写的代码片段:
I am currently working on a project in which I need to use FP-Growth algorithm. I know Weka is a handy tool for it. However, I am using C# for coding (due to some other libraries I need). So, I converted weka.jar
to weka.dll
using IKVM.NET
. Below is a code snippet that i have written:
FPGrowth FPMiner = new FPGrowth();
FPMiner.buildAssociations(dataset);
AssociationRules rules = FPMiner.getAssociationRules();
List<AssociationRule> rule = rules.getRules();
这给了我一个错误:
无法将类型 'java.util.List' 隐式转换为'System.Collections.Generic.List'.存在显式转换(您是否缺少强制转换?)
Cannot implicitly convert type 'java.util.List' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)
所以,我在最后一行添加了一个演员表:
So, I added a cast to the last line as:
List<AssociationRule> rule = (System.Collections.Generic.List<AssociationRule>)rules.getRules();
错误消失了,但运行代码时出现异常,说:
The error goes away but I get an exception when I run my code, saying:
System.InvalidCastException 未处理 Message=无法转换要键入的java.util.ArrayList"类型的对象System.Collections.Generic.List`1[weka.associations.AssociationRule]'.源=WindowsFormsApplication1
System.InvalidCastException was unhandled Message=Unable to cast object of type 'java.util.ArrayList' to type System.Collections.Generic.List`1[weka.associations.AssociationRule]'. Source=WindowsFormsApplication1
堆栈跟踪如下:
StackTrace:
at DetectGroup.Form1.GenerateARFF() in C:\Users\user\Documents\Visual Studio 2010\Projects\DetectGroup\DetectGroup\Form1.cs:line 279
at DetectGroup.Form1.findNearestNeighbours() in C:\Users\user\Documents\Visual Studio 2010\Projects\DetectGroup\DetectGroup\Form1.cs:line 236
at DetectGroup.Form1.findSelectedTraj() in C:\Users\user\Documents\Visual Studio 2010\Projects\DetectGroup\DetectGroup\Form1.cs:line 165
at DetectGroup.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\user\Documents\Visual Studio 2010\Projects\DetectGroup\DetectGroup\Form1.cs:line 404
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at DetectGroup.Program.Main() in C:\Users\user\Documents\Visual Studio 2010\Projects\DetectGroup\DetectGroup\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart() InnerException:
我现在不知道该怎么做.我试过搜索东西,但还没有得到解决方案.我知道错误是因为 getRules()
返回 java.util.List
而我试图将它用作 System.Collections.Generic.List代码>.我能做些什么来避免它?任何帮助都会很棒!
I am unable to figure out what to do now. I have tried searching stuff but haven't yet got the solution. I understand that the error is because getRules()
returns java.util.List
while I am trying to use it as System.Collections.Generic.List
. What can I do to avoid it? Any help would be great!
另外,C# 中是否有可用的数据挖掘库(如 Weka)?
Also, is there any data mining library (like Weka) available in C#?
谢谢!
我正在回答我自己的问题.我使用这个链接来解决我面临的问题.感谢@SecretSquirrel(见评论)和@Jon Iles(见我链接的答案).
I am answering my own question. I used this link to solve the problem I was facing. Thanks @SecretSquirrel(see the comments) and @Jon Iles (see the answer I've linked).