Course Content
Java Basics
Java Basics
Using Loops with Arrays
How to Iterate Through an Array Using Loops?
Arrays and loops are frequently used in tandem. When dealing with a large array containing 100 or even 1000 elements, manually working with and extracting each element would be impractical. Just imagine how time-consuming it would be to manually fill such an array...
To achieve such tasks, we'll employ loops. In the previous section, we observed that we initially assigned the variable i
a value of 0
in the for
loop, and array indexing also starts at 0
. Do you notice the connection?
Let's say we have a task to display all the elements of an array of type char
with a length of 10
. Let's examine a code fragment that accomplishes this task:
Main
package com.example; public class Main { public static void main(String[] args) { //initializing our char type array char[] charArray = {'c', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; //printing our array using for-loop for (int i = 0; i < charArray.length; i++) { System.out.print(charArray[i]); } } }
Note
System.out.print
prints the value to the console without moving to a new line after the output.
Let's take a closer look at how the loop iterates over an array:
Main
package com.example; public class Main { public static void main(String[] args) { //initializing our char type array char[] charArray = {'c', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; //printing our array using for-loop for (int i = 0; i < charArray.length; i++) { System.out.println("Now variable i = " + i + ", and value of charArray[" + i + "] = " + charArray[i] + ";"); /*the value of "i" is incrementing with every loop iteration*/ } } }
Task
Let's practice working with loops and arrays on your own. Your task is to populate an array with numbers from 1 to 10 using a for loop and then display it on the console. Remember that the numbers should be arranged in ascending order.
Thanks for your feedback!
Using Loops with Arrays
How to Iterate Through an Array Using Loops?
Arrays and loops are frequently used in tandem. When dealing with a large array containing 100 or even 1000 elements, manually working with and extracting each element would be impractical. Just imagine how time-consuming it would be to manually fill such an array...
To achieve such tasks, we'll employ loops. In the previous section, we observed that we initially assigned the variable i
a value of 0
in the for
loop, and array indexing also starts at 0
. Do you notice the connection?
Let's say we have a task to display all the elements of an array of type char
with a length of 10
. Let's examine a code fragment that accomplishes this task:
Main
package com.example; public class Main { public static void main(String[] args) { //initializing our char type array char[] charArray = {'c', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; //printing our array using for-loop for (int i = 0; i < charArray.length; i++) { System.out.print(charArray[i]); } } }
Note
System.out.print
prints the value to the console without moving to a new line after the output.
Let's take a closer look at how the loop iterates over an array:
Main
package com.example; public class Main { public static void main(String[] args) { //initializing our char type array char[] charArray = {'c', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; //printing our array using for-loop for (int i = 0; i < charArray.length; i++) { System.out.println("Now variable i = " + i + ", and value of charArray[" + i + "] = " + charArray[i] + ";"); /*the value of "i" is incrementing with every loop iteration*/ } } }
Task
Let's practice working with loops and arrays on your own. Your task is to populate an array with numbers from 1 to 10 using a for loop and then display it on the console. Remember that the numbers should be arranged in ascending order.
Thanks for your feedback!
Using Loops with Arrays
How to Iterate Through an Array Using Loops?
Arrays and loops are frequently used in tandem. When dealing with a large array containing 100 or even 1000 elements, manually working with and extracting each element would be impractical. Just imagine how time-consuming it would be to manually fill such an array...
To achieve such tasks, we'll employ loops. In the previous section, we observed that we initially assigned the variable i
a value of 0
in the for
loop, and array indexing also starts at 0
. Do you notice the connection?
Let's say we have a task to display all the elements of an array of type char
with a length of 10
. Let's examine a code fragment that accomplishes this task:
Main
package com.example; public class Main { public static void main(String[] args) { //initializing our char type array char[] charArray = {'c', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; //printing our array using for-loop for (int i = 0; i < charArray.length; i++) { System.out.print(charArray[i]); } } }
Note
System.out.print
prints the value to the console without moving to a new line after the output.
Let's take a closer look at how the loop iterates over an array:
Main
package com.example; public class Main { public static void main(String[] args) { //initializing our char type array char[] charArray = {'c', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; //printing our array using for-loop for (int i = 0; i < charArray.length; i++) { System.out.println("Now variable i = " + i + ", and value of charArray[" + i + "] = " + charArray[i] + ";"); /*the value of "i" is incrementing with every loop iteration*/ } } }
Task
Let's practice working with loops and arrays on your own. Your task is to populate an array with numbers from 1 to 10 using a for loop and then display it on the console. Remember that the numbers should be arranged in ascending order.
Thanks for your feedback!
How to Iterate Through an Array Using Loops?
Arrays and loops are frequently used in tandem. When dealing with a large array containing 100 or even 1000 elements, manually working with and extracting each element would be impractical. Just imagine how time-consuming it would be to manually fill such an array...
To achieve such tasks, we'll employ loops. In the previous section, we observed that we initially assigned the variable i
a value of 0
in the for
loop, and array indexing also starts at 0
. Do you notice the connection?
Let's say we have a task to display all the elements of an array of type char
with a length of 10
. Let's examine a code fragment that accomplishes this task:
Main
package com.example; public class Main { public static void main(String[] args) { //initializing our char type array char[] charArray = {'c', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; //printing our array using for-loop for (int i = 0; i < charArray.length; i++) { System.out.print(charArray[i]); } } }
Note
System.out.print
prints the value to the console without moving to a new line after the output.
Let's take a closer look at how the loop iterates over an array:
Main
package com.example; public class Main { public static void main(String[] args) { //initializing our char type array char[] charArray = {'c', 'o', 'd', 'e', 'f', 'i', 'n', 'i', 't', 'y'}; //printing our array using for-loop for (int i = 0; i < charArray.length; i++) { System.out.println("Now variable i = " + i + ", and value of charArray[" + i + "] = " + charArray[i] + ";"); /*the value of "i" is incrementing with every loop iteration*/ } } }
Task
Let's practice working with loops and arrays on your own. Your task is to populate an array with numbers from 1 to 10 using a for loop and then display it on the console. Remember that the numbers should be arranged in ascending order.