Table of Contents
Java code to print array elements using while
In this article, we will discuss the concept of Java code to print array elements using while
In this post, we are going to learn how to write a program to print in an array using while loop in Java language
Code to Display array elements
Program to print elements of an array using while loop – #1
In this code, we are going to learn how to display array elements in an array using while loop in Java language
Program 1
//Display integer of an array using for loop class Disp_Array_int_whileloop{ public static void main (String args[]){ int arr[]=new int[]{13,61,19,11,67,18}; //diclaration and initialization of an array System.out.println("Display elements of given array"); //for loop use to displays the elements by incrementing value of i int i=0; while(i<arr.length){ System.out.print(arr[i]+" "); i++; } } }
When the above code is executed, it produces the following result
Display elements of given array 13 61 19 11 67 18
Program to print elements of an array using while loop – #2
In this code, we are going to learn how to display array elements in an array using while loop in Java language
Program 2
//Display integer of an array using while loop class Disp_Array_int_whileloop1{ public static void main (String args[]){ int arr[]=new int[6]; arr[0]=120; arr[1]=130; arr[2]=140; arr[3]=150; arr[4]=160; arr[5]=170; //diclaration and initialization of an array System.out.println("Display elements of given array"); //while loop use to displays the elements by incrementing value of i int i=0; while(i<arr.length){ System.out.println(arr[i]+" "); i++; } } }
When the above code is executed, it produces the following result
Display elements of given array 120 130 140 150 160 170
Program to print Strings of an array using while loop – #1
In this code, we are going to learn how to display String array elements in 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]="Canada"; str[1]="UK"; str[2]="USA"; str[3]="India"; str[4]="UAE"; //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 Canada UK USA India UAE
Program to print Strings of an array using while loop – #2
In this code, we are going to learn how to display String array elements in an array using while loop in Java language
Program 2
//Java program to read and print string of an array using while class Disp_Array_of_String_While2{ public static void main (String args[]){ String str[]=new String[]{"China","India","Brazil","Japan","Egypt"}; //diclaration and creation of an array 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: China India Brazil Japan Egypt
Program to print character elements of an array using while loop – #1
In this code, we are going to learn how to display character array elements in an array using while loop in Java language
Program 1
//Print character of an array using while loop class Disp_Array_Char_Whileloop{ public static void main (String args[]){ char Char[]=new char[6]; //Declaration and creation of character array Char[0]='X'; Char[1]='Y'; Char[2]='Z'; Char[3]='A'; Char[4]='B'; Char[5]='C'; //initialization of characters index wise System.out.println("\n characters are:"); int j=0; while(j<Char.length){ System.out.println(Char[j]); j++; }//display all characters } }
When the above code is executed, it produces the following result
characters are: X Y Z A B C
Program to print character elements of an array using while loop – #2
In this code, we are going to learn how to display character array elements in an array using while loop in Java language
Program 2
//Print character of an array using while loop class Disp_Array_Char_Whileloop1{ public static void main (String args[]){ char Char[]=new char[]{'K','l','M','n','O','p'}; //Diclaration and creation of character array //initialization of characters index wise System.out.println("\n characters are:"); int j=0; while(j<Char.length){ System.out.println(Char[j]); j++; }//display all characters } }
When the above code is executed, it produces the following result
characters are: K l M n O p
Suggested for you
Similar post
Java program to read and print array elements using for loop
Java program to read and print array elements using while loop
Java program to read and print array elements using Do-while loop
C code to take and print array elements using for loop
C code to take and print array elements using while loop
C code to take and print array elements using do-while loop
C++ program to read and print array elements using for loop
C++ program to read and print array elements using while loop
C++ program to read and print array elements using do-while loop