Computer Science, asked by rajveer1332, 1 year ago

How to loop through an array in Java?

Answers

Answered by priyankabhasin327
0

Answer:

Explanation:

) You can use any loop e.g. for, while, and do-while or enhanced for loop to loop over an array. 2) If you need a counter to hold the current index of the array to implement your algorithm than use the for loop. 3) If your looping depends upon current element then use while and do-while loop.

Answered by Oreki
0

Given, an Array,

   int[ ] myArray = {1, 2, 3, 4, 5};

We can iterate over it by,

Using for loop:

   for (int i = 0; i < myArray.length; i++) {

           System.out.println(myArray[i]);

   }

   Can iterate in reverse - Yes.

Using for-each loop:

   for (int number : myArray) {

           System.out.println(number);

   }

   Can iterate in reverse - No.

Similar questions