Q:

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

belongs to collection: Java Array Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Array Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to find the length of an array... >>
<< Java program to count total positives, negatives a...