Skip to main content

Arrays

Iterating Through Arrays

Looping through an array means going through each item in the array. There are two common ways to do this in Java.

Using a For Loop:

String[] friends = {"Rahul", "Simran", "Kiran", "Naina", "Aman"};
for (int i = 0; i < friends.length; i++) {
  System.out.println(friends[i]);
}

This code uses a for loop. It's like saying, "Start from the first name, go up to the last name, and print each one."

Using a For-Each Loop:

String[] friends = {"Rahul", "Simran", "Kiran", "Naina", "Aman"};
for (String friend : friends) {
  System.out.println(friend);
}

This code uses a for-each loop. It's simpler. It says, "For each car in the list, print it." No need to worry about counting or length.

Here's what's happening:

  • for is like saying "Let's do something repeatedly."
  • friends is the name of our array with some names in it.
  • String friend is like a temporary box to hold each string as we look at them one by one.

So, what happens in each round of the loop:

  • 1st time: It looks at the first name (Rahul) and prints it.
  • 2nd time: It looks at the second name (Simran) and prints it.
  • 3rd time: It looks at the third name (Kiran) and prints it.
  • 4th time: It looks at the fourth name (Naina) and prints it.
  • 5th time: It looks at the fifth name (Aman) and prints it.

That's it! The for-each loop makes it easy to check out all the names in an array without getting into the nitty-gritty details of the array's size and index. It's like saying, "Hey, show me each number, one at a time!"

Practice problem

Write a Java program to display the sum of all the elements in an array.

Input

Array size - 7
1 2 3 4 5 6 7

Output

Sum of all the elements - 28
Approach1 using a for loop
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int size, sum = 0;

        // Create a Scanner object for user input
        Scanner scanner = new Scanner(System.in);

        // Ask the user for the size of the array
        System.out.print("Enter the size of the array: ");
        size = scanner.nextInt();

        // Create an array to store user-input elements
        int array[] = new int[size];

        // Prompt the user to enter array elements
        System.out.println("Enter array elements:");

        // Loop to get input elements and calculate the sum
        for (int i = 0; i < size; i++) {
            array[i] = scanner.nextInt();
            sum = sum + array[i];
        }

        // Display the sum of the array
        System.out.println("Sum of the array is: " + sum);
    }
}
Approach2 using a for-each loop
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int size;

        // Create a Scanner object for user input
        Scanner scanner = new Scanner(System.in);

        // Ask the user for the size of the array
        System.out.print("Enter the size of the array: ");
        size = scanner.nextInt();

        // Create an array to store user-input elements
        int array[] = new int[size];

        // Prompt the user to enter array elements
        System.out.println("Enter array elements:");

        // Loop to get input elements and calculate the sum
        for (int i = 0; i < size; i++) {
            array[i] = scanner.nextInt();
        }

        int sum = 0;
		for(int num : array){
		   sum = sum + num;
		}

        // Display the sum of the array
        System.out.println("Sum of the array is: " + sum);
    }
}