示例:
广告 该类别中的热门课程 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中文网其他相关文章!