×

Java Programs

Java Practice

Java program to access elements of character array using for each loop

Given/input a character array, we have to access elements of character array using for each loop.
By Nidhi Last updated : December 23, 2023

Problem statement

In this program, we will create an array of characters and access its elements using for each loop and print it on the screen.

Java program to access elements of character array using for each loop

The source code to access elements of the character array using "for each" loop is given below. The given program is compiled and executed successfully.

// Java program to access elements of character 
// array using for each loop

public class Main {
  public static void main(String[] args) {
    char charArray[] = {'A', 'B', 'C', 'D', 'E', 'F'};

    System.out.println("Array elements: ");
    for (char ch: charArray)
      System.out.println(ch);
  }
}

Output

Array elements: 
A
B
C
D
E
F

Explanation

In the above program, we created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we created an array of characters and initialized it with some character values. Then we accessed array elements one by one using "for each" or enhanced loop and printed them.

Java Array Programs »

More Java Array Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.