首页 > 解决方案 > Android Java 添加了一个 ArrayList 现在程序运行很慢

问题描述

我添加了一个 Arraylist 并减慢了我的代码。我的原始代码看起来像这样:

 //to draw the tiles
        for (int i = 0; i < bitmaps.length; i++) {

            for (int a = 0; a < bitmaps[i].length; a++) {

                if (bitmaps[i][a] != null) {

                    //to make sure tiles only get drawn on the screen
                    if(tileRect[i][a].left > RunMovement.getCurrentView() -25 && tileRect[i][a].left < RunMovement.getCurrentView() + screenWidth + 25) {


                        mCanvas.drawBitmap(bitmaps[i][a], null, tileRect[i][a], null);

                    }

                }
            }
        }

我需要添加一个Arraylist来存储不同的级别,所以我把它改成了这个

 //for the new array levels
        for (int b = 0; b < levelTileList.size(); b++) {               

            //to draw the tiles

            for (int i = 0; i < levelTileList.get(b).length; i++) {



                for (int a = 0; a < levelTileList.get(b)[i].length; a++) {//hope this works



                    if (levelTileList.get(b)[i][a] != null) {



                        //to make sure tiles only get drawn on the screen

                        if (levelTileListRec.get(b)[i][a].left > RunMovement.getCurrentView() - 25 && levelTileListRec.get(b)[i][a].left < RunMovement.getCurrentView() + screenWidth + 25) {


                            //mCanvas.drawBitmap(bitmaps[i][a], null, tileRect[i][a], null);
                            mCanvas.drawBitmap(levelTileList.get(b)[i][a], null, levelTileListRec.get(b)[i][a], null);


                        }

                    }
                }
            }
        }

该代码有效,但我的程序大大减慢了速度。有人可以告诉我为什么,如果有办法解决它。

标签: javaandroid

解决方案


一般来说,列表是比数组慢得多的数据结构,就初始化时间和访问时间而言,这只是因为它们的实现方式。


推荐阅读