警告说,我应该使用SparseArray&LT;字符串&GT;而不是HashMap中 [英] Warning says I should use SparseArray<String> instead of HashMap
问题描述
我是新来的Android的开发。在我的应用程序,我使用的HashMap
,但我得到以下警告:
使用新的
SparseArray&LT;字符串&GT;(...)
,而不是有更好的表现。
使用新的
SparseArray&LT;字符串&GT;(...)
,而不是有更好的表现。
您也越来越因故此警告这里描述。
SparseArrays整数到对象。不同于普通的数组 目的,可在指数间隙。它的目的是要更 效率比使用HashMap来整数映射为对象。
现在
我如何使用SparseArray?
您可以通过以下方式做到这一点:
-
的HashMap
办法:地图&LT;整数,位图&GT; _bitmapCache =新的HashMap&LT;整数,位图&GT;(); 私人无效fillBitmapCache(){ _bitmapCache.put(R.drawable.icon,BitmapFactory.de codeResource(getResources(),R.drawable.icon)); _bitmapCache.put(R.drawable.abstrakt,BitmapFactory.de codeResource(getResources(),R.drawable.abstrakt)); _bitmapCache.put(R.drawable.wallpaper,BitmapFactory.de codeResource(getResources(),R.drawable.wallpaper)); _bitmapCache.put(R.drawable.scissors,BitmapFactory.de codeResource(getResources() } 位图BM = _bitmapCache.get(R.drawable.icon);
-
SparseArray
办法:SparseArray&LT;位图&GT; _bitmapCache =新SparseArray&LT;位图&GT;(); 私人无效fillBitmapCache(){ _bitmapCache.put(R.drawable.icon,BitmapFactory.de codeResource(getResources(),R.drawable.icon)); _bitmapCache.put(R.drawable.abstrakt,BitmapFactory.de codeResource(getResources(),R.drawable.abstrakt)); _bitmapCache.put(R.drawable.wallpaper,BitmapFactory.de codeResource(getResources(),R.drawable.wallpaper)); _bitmapCache.put(R.drawable.scissors,BitmapFactory.de codeResource(getResources() } 位图BM = _bitmapCache.get(R.drawable.icon);
希望这将有助于。
I am new to developing in Android. In my app I'm using HashMap
, but I'm getting the following warning:
Use new
SparseArray<String>(...)
instead for better performance
Use new
SparseArray<String>(...)
instead for better performance
You are getting this warning because of reason described here.
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.
Now
how i use SparseArray ?
You can do it by below ways:
HashMap
way: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
way: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);
Hope it Will Help.
这篇关于警告说,我应该使用SparseArray&LT;字符串&GT;而不是HashMap中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!