警告说,我应该使用SparseArray<字符串>而不是HashMap中 [英] Warning says I should use SparseArray<String> instead of HashMap

查看:121
本文介绍了警告说,我应该使用SparseArray<字符串>而不是HashMap中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid的开发。在我的应用程序,我使用的HashMap ,但我得到以下警告:

  

使用新的 SparseArray<字符串>(...),而不是有更好的表现。

解决方案
  

使用新的 SparseArray<字符串>(...),而不是有更好的表现。

您也越来越因故此警告这里描述

  

SparseArrays整数到对象。不同于普通的数组   目的,可在指数间隙。它的目的是要更   效率比使用HashMap来整数映射为对象。

现在

  

我如何使用SparseArray?

您可以通过以下方式做到这一点:

  1. 的HashMap 办法:

     地图<整数,位图> _bitmapCache =新的HashMap<整数,位图>();
    私人无效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);
     

  2. SparseArray 办法:

      SparseArray<位图> _bitmapCache =新SparseArray<位图>();
    私人无效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:

  1. 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);
    

  2. 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆