简体   繁体   English

为什么Android API要求结果参数而不是返回方法?

[英]Why does Android API ask for a results parameter as opposed to a return method?

My question is throughout the whole of the Android API a lot of the library calls ask for a results[] parameter to be passed into method for example: 我的问题是在整个Android API中,很多库调用要求将results[]参数传递给方法,例如:

public boolean isInProximity(double startLat, double startLongitude, float meters) {
    float[] results = new float[3];
    Location.distanceBetween(startLat, startLongitude, endLatitude, endLongitude, results);
    return results[0] <= meters;
}

The call to android is the Location.distanceBetween() and it requires the results[] why don't they just include a return signature to the method? 对android的调用是Location.distanceBetween() ,它需要results[]为什么它们只是在方法中包含一个返回签名? I am assuming that it is some kind of memory or speed optimisation. 我假设它是某种内存或速度优化。 I am unsure on the exact reasoning behind it. 我不确定背后的确切原因。

You are correct, it is about optimization, that way you can call this method many times without needing to allocate memory each time. 你是对的,它是关于优化的,这样你可以多次调用这个方法,而不需要每次都分配内存。

Edit -- Here is a test so: http://codeyarns.com/2010/10/21/c-return-value-versus-output-parameter/ 编辑 - 这是一个测试: http//codeyarns.com/2010/10/21/c-return-value-versus-output-parameter/
Better late than never I guess. 迟到总比我想的要好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM