導航:首頁 > 編程語言 > java循環數組元素

java循環數組元素

發布時間:2023-12-27 06:44:34

1. java如何循環輸出數組

有兩種方法:
1. 使用三層循環遍歷多維數組
public class Ransack {
public static void main(String[] args) {
int array[][][] = new int[][][]{ // 創建並初始化數組
{ { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } },
{ { 13, 14, 15 }, { 16, 17, 18 } }
};
array[1][0][0] = 97; // 改變指定數組元素
for (int i = 0; i < array.length; i++) { // 遍歷數組
for (int j = 0; j <鏈盯 array[0].length; j++) {
for (int k = 0; k < array[0][0].length; k++) {
System.out.print(array[i][j][k] + "棚猛和\t");
}
System.out.println(); // 輸出一維數組後換行
}
}
}

2.使用foreach 遍歷三維數組
public class ForEachRansack {
public static void main(String[] args) {
int array[][][] = new int[][][]{ // 創建並初始化數組
{ { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } },
{ { 13, 14, 15 }, { 16, 17, 18 } }
};
for (int[][] is : array) { // 遍歷數組
for (int[] is2 : is) {
for (int i : is2) {
System.out.print(i + "\t");
}
System.out.println(); // 輸出知汪一維數組後換行
}
}
}
}

2. java中for或foreach是如何遍歷數組的

String[]array={"1","2","3","4","5"};
//for循環
for(inti=0;i<array.length;i++){
System.out.println(array[i]);
}

//foreach不是java裡面的關鍵字,foreache循環一般是指這個
for(Stringstring:array){
System.out.println(string);
}

3. JAVA如何遍歷數組

int[] is = new int[1,12,4,546]
for(int i=0; i<is.length; i++){
System.out.println(int[i] + "");
}
這就是一個最簡單的遍歷數組,遍歷數組通俗點就是說把數組中的每個數都讀一遍(部分有if條件+break的則可能不會完全遍歷),你可以對於數組中的每個數進行處理,亦或者是找到數組中那個你需要的數。

閱讀全文

與java循環數組元素相關的資料

熱點內容
資料庫查詢系統源碼 瀏覽:618
php5314 瀏覽:359
完美國際安裝到哪個文件夾 瀏覽:671
什麼app可以掃一掃做題 瀏覽:541
程序員編碼論壇 瀏覽:927
淘點是什麼app 瀏覽:662
中國高等植物pdf 瀏覽:455
51單片機時間 瀏覽:185
後台如何獲取伺服器ip 瀏覽:269
單片機流水燈程序c語言 瀏覽:237
程序員第二職業掙錢 瀏覽:242
運行里怎麼輸入伺服器路徑 瀏覽:844
pythonstepwise 瀏覽:513
劉一男詞彙速記指南pdf 瀏覽:67
php認證級別 瀏覽:372
方舟編譯啥時候推送 瀏覽:1013
php手機驗證碼生成 瀏覽:678
哲學思維pdf 瀏覽:19
凌達壓縮機有限公司招聘 瀏覽:537
weblogic命令部署 瀏覽:40