警告显示当我使用哈希映射在android中(使用新的SparseArray< String>) [英] Warning shows when i use Hash Map In android(Use new SparseArray<String>)
问题描述
我是在android中开发的新手。在我的Android应用程序中,我正在使用 HashMap
,但我收到警告:
I am new to developing in android. In my android app I'm using HashMap
, but I'm getting a warning:
**"Use new SparseArray<String>(...) instead for better performance"**
这是什么意思,我该如何使用 SparseArray< String>
来代替?
What does this mean, and how can I use SparseArray<String>
instead?
推荐答案
使用新的
SparseArray< String>(...)
代替更好的表现
由于此处。
You are getting this warning because of reason described here.
SparseArrays将整数映射到对象。与
对象的普通数组不同,索引中可能存在空白。它的目的是比使用HashMap将整数映射到对象更高效
。
SparseArrays map integers to Objects. Unlike a normal array of Objects, there can be gaps in the indices. It is intended to be more efficient than using a HashMap to map Integers to Objects.
现在
我如何使用SparseArray?
how i use SparseArray ?
您可以通过以下方式:
You can do it by below ways:
-
HashMap
方式:
Map<Integer, Bitmap> _bitmapCache = new HashMap<Integer, Bitmap>();
private void fillBitmapCache() {
_bitmapCache.put(R.drawable.icon, BitmapFactory.decodeResource(getResources(), R.drawable.icon));
_bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt));
_bitmapCache.put(R.drawable.wallpaper, BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper));
_bitmapCache.put(R.drawable.scissors, BitmapFactory.decodeResource(getResources(),
}
Bitmap bm = _bitmapCache.get(R.drawable.icon);
SparseArray
方式:
SparseArray<Bitmap> _bitmapCache = new SparseArray<Bitmap>();
private void fillBitmapCache() {
_bitmapCache.put(R.drawable.icon, BitmapFactory.decodeResource(getResources(), R.drawable.icon));
_bitmapCache.put(R.drawable.abstrakt, BitmapFactory.decodeResource(getResources(), R.drawable.abstrakt));
_bitmapCache.put(R.drawable.wallpaper, BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper));
_bitmapCache.put(R.drawable.scissors, BitmapFactory.decodeResource(getResources(),
}
Bitmap bm = _bitmapCache.get(R.drawable.icon);
希望它能帮上忙。
这篇关于警告显示当我使用哈希映射在android中(使用新的SparseArray< String>)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!