Usando Loops Com 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...
Como Iterar Por um Array Usando Loops?
Arrays e loops são frequentemente usados em conjunto. Ao lidar com um grande array contendo 100 ou até 1000 elementos, trabalhar e extrair manualmente cada elemento seria impraticável. Basta imaginar quanto tempo seria necessário para preencher manualmente um array tão grande...
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]); } } }
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*/ } } }
Swipe to start coding
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.
Solução
solution
Obrigado pelo seu feedback!