Codeforcoding

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)

Print Character array elements in JavaHere,

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

do-while loop in Java

Single dim array in Java

Data type and variable in Java

Operator in Java

loops in Java language

 

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

 

 

Program to Display String array elements in Java
Program to create and print an array of string in C
Exit mobile version