首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

Android 陣列循環取模運算:%(取餘)實現循環索引

     編輯:程式設計疑難解答

程式碼片段

mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
int question = mQuestionBank[mCurrentIndex].getmQuestion();
mQuestionTextView.setText(question);

為何需要取模運算?

實現陣列索引循環 (Circular Array),防止索引越界。

情境分析

  • 陣列長度 length = 4 (索引 0~3)
  • mCurrentIndex 初始為 0
  • 每次點擊按鈕索引 +1

無取模運算後果

點擊次數mCurrentIndex (無 %)結果
11正常
22正常
33正常
44❌ ArrayIndexOutOfBoundsException (最大索引 3)
55崩潰

有取模運算效果

點擊次數計算式mCurrentIndex對應題目
1(0+1)%41第 2 題
2(1+1)%42第 3 題
3(2+1)%43第 4 題
4(3+1)%40第 1 題 (循環回頭)
5(0+1)%41第 2 題
.........無限循環

數學原理

(index + 1) % length 將無限遞增的自然數映射至有限範圍 [0, length-1]:

  • a % b 餘數範圍:0 ~ b-1
  • 當 index = length - 1 時:(length-1 + 1) % length = 0
  • 形成閉環:0 → 1 → ... → length-1 → 0 → ...

進階:雙向循環 (上一題/下一題)

// 下一題 (遞增循環)
mCurrentIndex = (mCurrentIndex + 1) % length;

// 上一題 (遞減循環) - 注意負數取模
mCurrentIndex = (mCurrentIndex - 1 + length) % length;
// 或
mCurrentIndex = Math.floorMod(mCurrentIndex - 1, length); // Java 8+ 推薦

常見變體

需求公式
步長 N 循環(index + N) % length
指定起始索引(startIndex + offset) % length
隨機打亂後循環先 Collections.shuffle(list),再用索引循環

實務應用場景

  • 題庫循環練習
  • 圖片輪播
  • 音樂播放清單循環
  • 日誌緩衝區 (Ring Buffer)
  • 負載均衡輪詢
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved