範例:
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
int A[]={1,2,4};
範例:
int[] A={1,2,4};
範例:
int[]A={1,2,4};
為什麼要迭代數組元素?
對數組元素執行任何特定操作,然後迭代元素並套用該操作。運算是對數組元素進行加法、乘法、除法、減法等。
陣列元素可以有多少種迭代方式?
如我們上面所討論的,數組元素可以透過 3 種方式進行迭代,因此基於循環功能數組可以迭代其元素。
文法:
int a[]=new int[size of an array]; for(int i=0;i<a.length;i++) { //array logic }
文法:
int a[]=new int[size of an array]; for(int i=0:a) { //array logic }
文法:
int a[]=new int[size of an array]; int i=0; for(i<a.length) { //array logic i++; }
以下是 Java 陣列迭代器的範例:
代碼:
public class Main { public static void main(String[] args) { //defining static array with 10 elements int arrayNumbers[] = {1,2,3,4,5,6,7,8,9,10}; //for loop for(int i=0;i<arrayNumbers.length;i++) { //displaying array index and its corresponding values System.out.println("arrayNumbers["+i+"]"+" index value :"+arrayNumbers[i]); } } }
輸出:
代碼:
import java.util.Scanner; public class Main { public static void main(String[] args) { // Scanner for taking input from user Scanner scanner = new Scanner(System.in); System.out.println("Enter size of the array =>"); // for integer input user nextInt() int input = scanner.nextInt(); // defining dynamic array with any required size elements int arrayNumbers[] = new int[input]; System.out.println("Enter array elements=>"); //for loop for adding elements to the array dynamically for(int i=0;i<arrayNumbers.length;i++) { arrayNumbers[i]=scanner.nextInt(); } System.out.println("Dynamic array elements are=>"); // for loop for (int i = 0; i < arrayNumbers.length; i++) { // displaying array index and its corresponding values System.out.println("arrayNumbers[" + i + "]" + " index value :" + arrayNumbers[i]); } //closing scanner object scanner.close(); } }
輸出:
代碼:
public class Main { public static void main(String[] args) { //defining static array with 7 elements double doubleNumbers[] = {12.0,14.0,15.21,13.57,69.72,11,13}; int temp=0; //for each loop for(double i:doubleNumbers) { //displaying array index and its corresponding values System.out.println("doubleNumbers["+temp+"]"+" index value :"+i); temp++;//temp is for storing index values as int type because array index never exist in double form } } }
輸出:
代碼:
import java.util.Scanner; public class ForEachDynamicArrayIterator { public static void main(String[] args) { // Scanner for taking input from user Scanner scanner = new Scanner(System.in); System.out.println("Enter size of the array =>"); // for integer input user nextInt() int input = scanner.nextInt(); // defining dynamic array with any required size elements double doubleNumbers[] = new double[input]; System.out.println("Enter array elements=>"); //for loop for adding elements to the array dynamically for(int i=0;i<doubleNumbers.length;i++) { doubleNumbers[i]=scanner.nextDouble(); } System.out.println("Dynamic array elements are=>"); int temp=0; // foreach loop for (double d:doubleNumbers) { // displaying array index and its corresponding values System.out.println("doubleNumbers[" + temp + "]" + " index value :" +d); temp++;//temp is for storing index values as int type because array index never exist in double form } //closing scanner object scanner.close(); } }
輸出:
代碼:
public class WhileStaticArrayIterator { public static void main(String[] args) { //defining static array with 6 elements String[] stringArray = {"Paramesh","Ramesh","Amardeep","Chaithanya","Krishna","Rama"}; int temp=0; //for each loop while(temp<stringArray.length) { //displaying array index and its corresponding values System.out.println("stringArray["+temp+"]"+" index value :"+stringArray[temp]); temp++;//temp is for storing index values } } }
輸出:
代碼:
import java.util.Scanner; public class WhileDynamicArrayIterator { public static void main(String[] args) { // Scanner for taking input from user Scanner scanner = new Scanner(System.in); System.out.println("Enter size of the array =>"); // for integer input user nextInt() int input = scanner.nextInt(); // defining string dynamic array with any required size elements String stringArray[] = new String[input]; System.out.println("Enter array string elements=>"); // for loop for adding elements to the array dynamically for (int i = 0; i < stringArray.length; i++) { stringArray[i] = scanner.next(); } System.out.println("Dynamic array elements are=>"); int temp = 0; // foreach loop while (temp<stringArray.length) { // displaying array index and its corresponding values System.out.println("stringArray[" + temp + "]" + " index value :" + stringArray[temp]); temp++;// temp is for storing index values } // closing scanner object scanner.close(); } }
輸出:
注意:對於每個循環,迭代時間比 while 和 for 循環少得多,因此每個循環優於 while 和 for 迴圈。可以使用 for 迴圈、for every 迴圈以及具有靜態和動態值的 while 迴圈來迭代數組。數組迭代用於對數組元素執行任何操作。
這是 Java 陣列迭代器的指南。在這裡,我們討論數組迭代器在 Java 中如何運作及其範例及其程式碼實作。您也可以瀏覽我們其他推薦的文章以了解更多資訊 –
以上是Java 數組迭代器的詳細內容。更多資訊請關注PHP中文網其他相關文章!