Android Java添加了一个ArrayList,现在程序运行非常缓慢。

huangapple 未分类评论50阅读模式
英文:

Android Java Added an ArrayList now program is running very slow

问题

我添加了一个 Arraylist,导致代码运行变慢。我的原始代码大致如下:

 //绘制图块
 for (int i = 0; i < bitmaps.length; i++) {

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

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

             //确保图块只在屏幕上绘制
             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 (int b = 0; b < levelTileList.size(); b++) {

     //绘制图块
     for (int i = 0; i < levelTileList.get(b).length; i++) {

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

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

                 //确保图块只在屏幕上绘制
                 if (levelTileListRec.get(b)[i][a].left > RunMovement.getCurrentView() - 25 && levelTileListRec.get(b)[i][a].left < RunMovement.getCurrentView() + screenWidth + 25) {

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

                 }

             }
         }
     }
 }

代码可以运行,但我的程序明显变得运行缓慢。有人能告诉我为什么吗?如果有方法可以修复吗?

英文:

I added a Arraylist and is slowed down my code.
My original code looked something like this:

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

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

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

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

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

                    }

                }
            }
        }

I needed to add a Arraylist to store different levels, so I changed it to this

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

            //to draw the tiles
          
            for (int i = 0; i &lt; levelTileList.get(b).length; i++) {
               

             
                for (int a = 0; a &lt; 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 &gt; RunMovement.getCurrentView() - 25 &amp;&amp; levelTileListRec.get(b)[i][a].left &lt; 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);
                            

                        }

                    }
                }
            }
        }

The code works but my program slowed down drastically.
Can someone please tell me why, and if there's a way to fix it.

答案1

得分: 0

列表,总的来说,是数据结构,初始化时间和访问时间都比数组慢得多,这仅仅是因为它们的实现方式。

英文:

Lists, in general, are data structures that are way slower than arrays, terms of initializing time and accessing time, just because the way they are implemented.

huangapple
  • 本文由 发表于 2020年4月8日 18:18:23
  • 转载请务必保留本文链接:https://java.coder-hub.com/61098334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定