java - 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);
}
}
}
}
}
该代码有效,但我的程序大大减慢了速度。有人可以告诉我为什么,如果有办法解决它。
解决方案
一般来说,列表是比数组慢得多的数据结构,就初始化时间和访问时间而言,这只是因为它们的实现方式。
推荐阅读
- sql - 根据多列计算出现次数,然后加入表
- node.js - Different Data for Different users in Mern and Jwt
- excel - Excel Graph should not have trailing empty cells take up space on axis
- html - 侧边栏Vuejs中“li”的活动类
- python - 只有在完全完成数据采集后才读取 hdf5 文件
- terraform - for k, v 循环在多级映射上进行过滤
- android - 仅在拖动时展开 AppBarLayout,但在甩动时不展开
- google-sheets - 在 google sheet 中正确排序 yyyy/mm 列(1991/10 总是在 1991/01 之后)
- r - 当我将数据框保存为 .txt 时缺少列
- python-requests - 无法使用 python 请求完成我的 SAML 登录