Array

Program to Display String array elements in Java

Program to Display String  array elements in Java

In this tutorial, we will discuss the concept of program to display String array elements in Java

In this topic, we are going to learn how to print String array elements in Java programming language  using for, while and do-while 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 String array elements

Here,

  • s1,s2,s3,s4 and s5 represent the string elements of the array
  • 0,1,2,3 and 4 represent the index of the array which is used to access the array elements
  • 1,2,3,4 and 5 represent the length of the array

Code to  print array elements of one dim array

Program to Display String array in Java using for loop

In this program, we are briefing how to print  String elements  of an array using for loop in Java language

Program 1

//Java program to read and print string of an array using for

class Array_of_String_For1{
public static void main (String args[]){
String str[]=new String[5]; 
//diclaration and creation of an array

str[0]="Robert";
str[1]="Joe";
str[2]="Mariya";
str[3]="Baswan";
str[4]="Saman";

//input the String input
System.out.println("\nYour strings are:");
for(int j=0; j<str.length; j++){
System.out.println(str[j]); 
}//display all entered string
}
}

When the above code is executed, it produces the following result

Your Strings are:

Robert
Joe
Mariya
Baswan
Saman

 

Program to Display String array in Java using while loop

In this program, we are briefing how to print  String of an array using while loop in Java language

Program 1

//Java program to read and print string of an array using while

class Disp_Array_of_String_While1{
public static void main (String args[]){
String str[]=new String[5]; 
//diclaration and creation of an array

str[0]="Markony";
str[1]="Smith";
str[2]="Pidan";
str[3]="Jorge";
str[4]="Monika";
//input the String input

System.out.println("\nYour strings are:");
int j=0;
while(j<str.length){
System.out.println(str[j]); 
 j++;
}//display all entered string
}
}

When the above code is executed, it produces the following result

Your Strings are:

Markony
Smith
Pidan
Jorge
Monika

 

Program to Display String array in Java using do-while loop

In this program, we are briefing how to print  Strings of an array using do-while loop in Java language

Program 3

//Java program to read and print string of an array using do-while

class Array_of_String_DoWhile1{
public static void main (String args[]){
String str[]=new String[5]; 
//diclaration and creation of an array

str[0]="Dony";
str[1]="Sangagara";
str[2]="Uvaraj";
str[3]="Soniya";
str[4]="Malinka";
//input the String input

System.out.println("\nYour strings are:");
int j=0;
do{
System.out.println(str[j]); 
 j++;
}while(j<str.length);
//display all entered string
}
}

When the above code is executed, it produces the following result

Your Strings are:

Dony
Sangagara
Uvaraj
Soniya
Malinka


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

 

Suggested for you

Array in Programming language

One dimensional array in C language

One dimensional array in C++ language

Two dimension array in Java language

Two dimension array in C language

Two dimension array in C++ language

Three dimension array in Java language

Three dimension array in C language

Three dimension array in C++ language

for loop in Java language

while loop in Java language

Do-while loop in Java language

Enhanced for loop in Java language

Data  type and variable in Java language

Operator in Java language

 

Program to print array elements in Java
Print Character array elements in Java
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago