Android 未经检查的分配
Android Unchecked assignment
我在我的应用程序中使用定位服务,在我设置权限的地方,我收到以下警告:未经检查的分配:'java.util.ArrayList' 到 'java.util.ArrayList
代码:
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
permissionsToRequest = findUnAskedPermissions(permissions);
警告出现在最后一行。
我在代码开头定义了这样的数组:
ArrayList<String> permissions = new ArrayList<>();
ArrayList<String> permissionsToRequest= new ArrayList<>();
ArrayList<String> permissionsRejected = new ArrayList<>();
稍后在我的代码中,我请求许可(如果用户不允许):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (permissionsToRequest.size() > 0) {
requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]),
ALL_PERMISSIONS_RESULT);
canGetLocation = false;
}
}
这里的 toArray
导致警告:Call to 'toArray()' with pre-sized array argument 'new String[permissionsToRequest.size()]
这里我可以尝试换成
toArray(new String[0])
但不确定是否正确。
对于 Unchecked assignment: 'java.util.ArrayList' to 'java.util.ArrayList
警告,我假设 findUnAskedPermissions
方法的 return 值只是 ArrayList
而不是 ArrayList<String>
(它应该是什么) .
关于数组的创建,在inspection hint of IDEA中有解释为什么首选toArray(new String[0])
:
There are two styles to convert a collection to an array: either using
a pre-sized array (like c.toArray(new String[c.size()])) or
using an empty array (like c.toArray(new String[0]).
In older Java versions using pre-sized array was recommended, as the
reflection call which is necessary to create an array of proper size
was quite slow. However since late updates of OpenJDK 6 this call was
intrinsified, making the performance of the empty array version the
same and sometimes even better, compared to the pre-sized version.
Also passing pre-sized array is dangerous for a concurrent or
synchronized collection as a data race is possible between the
size and toArray call which may result in extra nulls at
the end of the array, if the collection was concurrently shrunk during
the operation.
有 in-depth analysis 种不同的创建数组的方法得出了这个结论:
toArray(new T[0]) seems faster, safer, and contractually cleaner, and
therefore should be the default choice now. Future VM optimizations
may close this performance gap for toArray(new T[size]), rendering the
current "believed to be optimal" usages on par with an actually
optimal one. Further improvements in toArray APIs would follow the
same logic as toArray(new T[0]) — the collection itself should create
the appropriate storage.
我在我的应用程序中使用定位服务,在我设置权限的地方,我收到以下警告:未经检查的分配:'java.util.ArrayList' 到 'java.util.ArrayList
代码:
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
permissionsToRequest = findUnAskedPermissions(permissions);
警告出现在最后一行。
我在代码开头定义了这样的数组:
ArrayList<String> permissions = new ArrayList<>();
ArrayList<String> permissionsToRequest= new ArrayList<>();
ArrayList<String> permissionsRejected = new ArrayList<>();
稍后在我的代码中,我请求许可(如果用户不允许):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (permissionsToRequest.size() > 0) {
requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]),
ALL_PERMISSIONS_RESULT);
canGetLocation = false;
}
}
这里的 toArray
导致警告:Call to 'toArray()' with pre-sized array argument 'new String[permissionsToRequest.size()]
这里我可以尝试换成
toArray(new String[0])
但不确定是否正确。
对于 Unchecked assignment: 'java.util.ArrayList' to 'java.util.ArrayList
警告,我假设 findUnAskedPermissions
方法的 return 值只是 ArrayList
而不是 ArrayList<String>
(它应该是什么) .
关于数组的创建,在inspection hint of IDEA中有解释为什么首选toArray(new String[0])
:
There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]).
In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation.
有 in-depth analysis 种不同的创建数组的方法得出了这个结论:
toArray(new T[0]) seems faster, safer, and contractually cleaner, and therefore should be the default choice now. Future VM optimizations may close this performance gap for toArray(new T[size]), rendering the current "believed to be optimal" usages on par with an actually optimal one. Further improvements in toArray APIs would follow the same logic as toArray(new T[0]) — the collection itself should create the appropriate storage.