- On
- By
- 0 Comment
- Categories: Array, Data types, do-while, for loop, Loop, While loop
Print Character array elements in Java
Print Character array elements in Java
In this tutorial, we will discuss the concept of Print Character array elements in Java
In this topic, we are going to learn how to print character array elements in Java programming language using loops.
In this blog, We have already discuss that “What is an array“, type of arrays and how to access it(one dim, two dim and three dim arrays)
Arrays are the special type of variables to store Multiple type of values(int, string,char, etc)under the same name in the continuous memory location. we can be easily accessed using the indices(indexes)
Here,
- C, H, A, R represent the character elements of the array
- 0,1,2 and 3 represent the index of the array which is used to access the array elements
- 1,2,3 and 4 represent the length of the array
Display Character array elements in Java
Program to Display Characters of an array in Java using for loop
In this program, we are briefing print character elements of an array using for loop in Java language
Program 1
//Print character of an array using for looop class Disp_Array_Char_For1{ public static void main (String args[]){ char Char[]=new char[6]; //declaration and creation of character array Char[0]='A'; Char[1]='B'; Char[2]='C'; Char[3]='D'; Char[4]='E'; Char[5]='F'; System.out.println("\n characters are:"); for(int j=0; j<Char.length; j++){ System.out.println(Char[j]); }//display all entered characters } }
When the above code is executed, it produces the following result
characters are: A B C D E F
Program to Display Characters of an array in Java using while loop
In this program, we are briefing print character elements of an array using while loop in Java language
Program 2
//Print character of an array using whiloe loop class Disp_Array_Char_While1{ public static void main (String args[]){ char Char[]=new char[6]; //diclaration and creation of character array Char[0]='a'; Char[1]='b'; Char[2]='c'; Char[3]='d'; Char[4]='e'; Char[5]='f'; System.out.println("\n characters are:"); int j=0; while(j<Char.length){ System.out.println(Char[j]); j++; }//display all entered characters } }
When the above code is executed, it produces the following result
characters are: a b c d e f
Program to Display Characters of an array in Java using do-while loop
In this program, we are briefing print character elements of an array using do- while loop in Java language
Program 3
//Print character of an array using whiloe loop class Disp_Array_Char_DoWhile1{ public static void main (String args[]){ char Char[]=new char[6]; //diclaration and creation of character array Char[0]='x'; Char[1]='y'; Char[2]='z'; Char[3]='b'; Char[4]='c'; Char[5]='f'; System.out.println("\n characters are:"); int j=0; do{ System.out.println(Char[j]); j++; }while(j<Char.length); //display all entered characters } }
When the above code is executed, it produces the following result
characters are: x y z b c f
Suggested for you
for loop in Java
while loop in Java
Data type and variable in Java
Operator in Java
Similar post
Code to read and print integers of an array in Java
Code to read and print strings of an array in Java
Code to read and print characters of an array in Java
Code to read and print integers of an array in C++
Code to read and print strings of an array in C++
Code to read and print characters of an array in C++
Code to read and print integers of an array in C
Code to read and print strings of an array in C