警告显示当我使用哈希映射在android中(使用新的SparseArray< String>) [英] Warning shows when i use Hash Map In android(Use new SparseArray<String>)

查看:269
本文介绍了警告显示当我使用哈希映射在android中(使用新的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:


  1. 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&lt; String&gt;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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