首页 \ 问答 \ 如何将单个对象[]传递给params对象[](How to pass a single object[] to a params object[])

如何将单个对象[]传递给params对象[](How to pass a single object[] to a params object[])

我有一个采用params object []的方法,如:

void Foo(params object[] items)
{
    Console.WriteLine(items[0]);
}

当我将两个对象数组传递给这个方法时,它工作正常:

Foo(new object[]{ (object)"1", (object)"2" }, new object[]{ (object)"3", (object)"4" } );
// Output: System.Object[]

但是当我传递单个对象[]时,它不会将我的对象[]作为第一个参数,而是将其所有元素都像我想要一个接一个地传递:

Foo(new object[]{ (object)"1", (object)"2" });
// Output: 1, expected: System.Object[]

如何将单个对象[]作为第一个参数传递给params数组?


I have a method which takes params object[] such as:

void Foo(params object[] items)
{
    Console.WriteLine(items[0]);
}

When I pass two object arrays to this method, it works fine:

Foo(new object[]{ (object)"1", (object)"2" }, new object[]{ (object)"3", (object)"4" } );
// Output: System.Object[]

But when I pass a single object[], it does not take my object[] as the first param, instead it takes its all elements like I wanted to pass them one by one:

Foo(new object[]{ (object)"1", (object)"2" });
// Output: 1, expected: System.Object[]

How do I pass a single object[] as a first argument to a params array?


原文:https://stackoverflow.com/questions/36350
更新时间:2021-05-13 13:05

最满意答案

在标准的计算模型中,这个问题可能没有解决方案明显优于O(n ^ 2)。

发现三个共线点的问题减少到找到最多点的线的问题,找到三个共线点是3SUM-硬,这意味着在小于O(n ^ 2)时间内解决它将是一个主要的理论结果。

查看上一个问题 ,找出三个共线点。

为了您的参考(使用已知的证明),假设我们想回答一个3SUM问题,例如在列表X中找到x,y,z,使得x + y + z = 0。如果我们有一个快速算法的共线点问题,我们可以使用该算法来解决3SUM问题如下。

对于X中的每个x,创建点(x,x ^ 3)(现在我们假设X的元素是不同的)。 接下来,检查创建点之间是否存在三条共线点。

要看到这样做,请注意,如果x + y + z = 0,则从x到y的线的斜率为

(y ^ 3-x ^ 3)/(y-x)= y ^ 2 + yx + x ^ 2

并且从x到z的线的斜率是

(z ^ 3-x ^ 3)/(z-x)= z ^ 2 + zx + x ^ 2 =( - (x + y))^ 2 - (x + y)x + x ^ 2 = 2 + 2xy + y ^ 2 - x ^ 2 - xy + x ^ 2 = y ^ 2 + yx + x ^ 2

相反,如果从x到y的斜率等于从x到z的斜率

y ^ 2 + yx + x ^ 2 = z ^ 2 + zx + x ^ 2,

这意味着

(y-z)(x + y + z)= 0,

所以y = z或z = -x-y可以证明减少有效。

如果X中有重复项,您首先检查x + 2y = 0,对于任何x和重复元素y(使用散列的线性时间或使用排序的O(n lg n)时间),然后在缩小到共线点查找问题。


There is likely no solution to this problem that is significantly better than O(n^2) in a standard model of computation.

The problem of finding three collinear points reduces to the problem of finding the line that goes through the most points, and finding three collinear points is 3SUM-hard, meaning that solving it in less than O(n^2) time would be a major theoretical result.

See the previous question on finding three collinear points.

For your reference (using the known proof), suppose we want to answer a 3SUM problem such as finding x, y, z in list X such that x + y + z = 0. If we had a fast algorithm for the collinear point problem, we could use that algorithm to solve the 3SUM problem as follows.

For each x in X, create the point (x, x^3) (for now we assume the elements of X are distinct). Next, check whether there exists three collinear points from among the created points.

To see that this works, note that if x + y + z = 0 then the slope of the line from x to y is

(y^3 - x^3) / (y - x) = y^2 + yx + x^2

and the slope of the line from x to z is

(z^3 - x^3) / (z - x) = z^2 + zx + x^2 = (-(x + y))^2 - (x + y)x + x^2 = x^2 + 2xy + y^2 - x^2 - xy + x^2 = y^2 + yx + x^2

Conversely, if the slope from x to y equals the slope from x to z then

y^2 + yx + x^2 = z^2 + zx + x^2,

which implies that

(y - z) (x + y + z) = 0,

so either y = z or z = -x - y as suffices to prove that the reduction is valid.

If there are duplicates in X, you first check whether x + 2y = 0 for any x and duplicate element y (in linear time using hashing or O(n lg n) time using sorting), and then remove the duplicates before reducing to the collinear point-finding problem.

相关问答

更多
  • 在标准的计算模型中,这个问题可能没有解决方案明显优于O(n ^ 2)。 发现三个共线点的问题减少到找到最多点的线的问题,找到三个共线点是3SUM-硬,这意味着在小于O(n ^ 2)时间内解决它将是一个主要的理论结果。 查看上一个问题 ,找出三个共线点。 为了您的参考(使用已知的证明),假设我们想回答一个3SUM问题,例如在列表X中找到x,y,z,使得x + y + z = 0。如果我们有一个快速算法的共线点问题,我们可以使用该算法来解决3SUM问题如下。 对于X中的每个x,创建点(x,x ^ 3)(现在我们 ...
  • 它看起来像截断而不是舍入到最近的像素。 很难看到那些小黑墨斑点。 你能发布代码吗? It looks like you are truncating instead of rounding to the nearest pixel. Difficult to see on those small black ink splotches. Could you post the code?
  • 计算连续点O(N)之间的斜率。 如果3个连续点在两对之间具有几乎相同的斜率,则3个点几乎在同一条线上(减去舍入误差)。 因此,要找到直线,只需寻找斜率几乎相等的连续点的部分。 您可以通过点O(N)一次完成此操作。 如果你有很多点,你可能会遇到这样的情况:曲线非常小,以至于它比你比较斜率时允许的舍入误差小。 如果您看到这个添加另一个检查以与直线中的第一个斜率进行比较,以确保您不会出现复合错误。 如果您需要更全面或更快速的内容,请考虑使用OpenCV。 Compute the slopes between co ...
  • 使用shapely可以找到交点,而不是使用point作为fsolve()的init猜测值来找到真正的解决方案: #for contour def p_0(num,t) : esc_p = np.sum((((-1)**n)*(np.exp(t)**n)*((math.factorial(n)*((n+1)**0.5))**-1)) for n in range(1,num,1)) return esc_p+1 tau = np.arange(-2,3,0.1) x,y= np.mesh ...
  • 鉴于你正在实现一个Yahtzee游戏,你可能需要测试除了小直道之外的其他模式,所以在调用函数之前创建值数组会更好,这样你就可以在所有测试中使用它们,而不是得到它们小直线测试中DOM元素的值。 无论如何,这是我想到的第一种方法,用于测试代表五个六面骰子值的数组中的小直线: // assume r is an array with the values from the dice r.sort(); if (/1234|2345|3456/.test(r.join("").replace ...
  • 编辑:我原本误解了这个问题,似乎。 至于验证路径:我认为只有一个函数可以确定一个点是否有效而不是事先计算所有值。 就像是: function getValidatorForPoints(x1, y1, x2, y2) { var slope = (y2 - y1) / (x2 - x1); return function (x, y) { return (y - y1) == slope * (x - x1); } } 然后,给出两点,你可以这样做: var isV ...
  • 这条线 double slope = (A.y-B.y)/(A.x-B.x); 是错的。 这会进行int除法,然后将结果转换为double 。 你要 double slope = (A.y-B.y)/(double)(A.x-B.x); The line double slope = (A.y-B.y)/(A.x-B.x); is wrong. This does int division, and then converts the result to a double. You want doub ...
  • 我会尝试一些事情: isqrt对于您正在工作的值范围无效。 只需使用浮点sqrt函数: isqrt = floor $ sqrt ((fromIntegral n) :: Double) 或者,不要计算整数平方根,而是在列表推导中使用这样的逻辑: x <- takeWhile (\x -> x*x <= n) [0..], y <- takeWhile (\y -> y*y <= n - x*x) [0..] 另外,我会使用像x*x而不是x^2这样的表达式。 最后,为什么不用这样的方法计算解决方案的数量 ...
  • BTW的复杂性实际上要降低O(n^3) ,你需要: 以某种方式排序点 按x和或y按升序或降序排列。 使用极坐标有时也会有所帮助 在impera(分而治之)算法中使用除法 通常对于平面几何算法,将区域划分为象限和子象限是个好主意,但这些算法难以在矢量图形上编码 还有另外一种加速可能性 检查所有可能的方向(有限的数量,例如仅360度),这导致O(n^2) 。 然后计算仍为O(m^3)结果,其中m是每个测试方向的点的子集。 好的,这是我用C ++编写的基本内容,例如: void points_on_line() ...
  • 好吧,让我们从两个堕落的案例开始: 如果你有0分,你可以随心所欲地回答(或者有一条线,或者没有这样的线) 如果你有1分或2分,答案总是肯定的 假设您有3个以上的积分,并且想要检查它们是否都在同一条线上。 取两个任意点(x1, y1)和(x2, y2) 。 你又有两个案例: x1 == x2 ; 检查所有点是否为xi == x1 x1 != x2 ; 检查所有点是否满足两个条件 : (y1 - y2) * (xi - x2) == (yi - y2) * (x1 - x2) (y2*x1 - y1 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。