需要帮助将代码片段从java转换为C#等价物

时间:2014-01-26 03:26:33

标签: java c# arraylist xamarin code-translation

这是我想要从Java翻译成C#的代码片段。我不确定导致错误的是什么,但我以前从未使用过ArrayLists和向量。在此先感谢!!

//Java class definitions, constructors, fields, methods etc here. 
//sphbasis is a Vector object.

    public SphericalHarmonicDecomposition[] getSphericalHarmonicBasis() {
    return (SphericalHarmonicDecomposition[])(sphbasislist.toArray(
    new SphericalHarmonicDecomposition[sphbasislist.size()]));
}

我尝试在C#中执行以下操作:

//C# class definitions, constructors, fields, methods etc here. 
//sphbasis is a ArrayList object.

    public SphericalHarmonicDecomposition[] getSphericalHarmonicBasis() {
    return (SphericalHarmonicDecomposition[])(sphbasislist.ToArray(
    new SphericalHarmonicDecomposition[sphbasislist.Count]));
    }

我收到以下错误。我在Mac上使用Mono和Xamarin工作室。

Error CS1502: The best overloaded method match for 
`System.Collections.ArrayList.ToArray(System.Type)' 
has some invalid arguments (CS1502) (projectx)

Error CS1503: Argument `#1' cannot convert    
`matdcal.engine.model.SphericalHarmonicDecomposition[]' expression 
to type `System.Type' (CS1503) (projectx)

1 个答案:

答案 0 :(得分:3)

请尝试以下方法。在Java中,您需要将数组传递给toArray方法,但在C#(.NET)中这是不正确的。

//C# class definitions, constructors, fields, methods etc here. 
//sphbasis is a ArrayList object.

    public SphericalHarmonicDecomposition[] getSphericalHarmonicBasis() {
    return (SphericalHarmonicDecomposition[])(sphbasislist.ToArray());
    }

参考

Java ArrayList.toArray

C# List.ToArray