Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Using Loops with Arrays | Arrays
Java Basics
course content

Course Content

Java Basics

Java Basics

1. Getting Started
2. Basic Types, Operations
3. Loops
4. Arrays
5. String

bookUsing 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:

java

Main

copy
123456789101112
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:

java

Main

copy
12345678910111213
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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
toggle bottom row

bookUsing 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:

java

Main

copy
123456789101112
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:

java

Main

copy
12345678910111213
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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
toggle bottom row

bookUsing 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:

java

Main

copy
123456789101112
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:

java

Main

copy
12345678910111213
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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

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:

java

Main

copy
123456789101112
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:

java

Main

copy
12345678910111213
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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt