ADVERTISEMENT
ADVERTISEMENT

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.
Submitted by Nidhi, on March 13, 2022

Problem Solution:

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

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.

Java Array Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.