導航:首頁 > 編程語言 > 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循環數組元素相關的資料

熱點內容
什麼是適合自己的app 瀏覽:451
雲伺服器怎麼補漏洞 瀏覽:176
如何看自己的安卓是什麼等級 瀏覽:633
被丁磊挖走的程序員 瀏覽:19
gsk980ta編程 瀏覽:507
單片機離線 瀏覽:84
解壓助眠敲擊外國長指甲 瀏覽:421
3次元編程 瀏覽:803
夫妻傳送英文命令 瀏覽:316
程序員題代做 瀏覽:950
戴爾伺服器怎麼擴大內存 瀏覽:641
解壓包密碼消失 瀏覽:135
昆侖通態觸摸屏用戶窗加密 瀏覽:564
農信app怎麼看日額 瀏覽:865
iphone備忘錄包含視頻無法加密 瀏覽:283
抖音快手紅包源碼 瀏覽:137
程序員資源分析 瀏覽:586
linux交換分區大小 瀏覽:922
mt怎麼編譯 瀏覽:337
qq郵箱打開pdf 瀏覽:461