Array

Program to print array elements in Java

Program to print array elements in Java

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

In this topic, we are going to learn how to print integer 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)

Display integer array elements

Here,

  • e1,e2,e3,e4 and e5 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 array in Java using for loop

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

Program 1

//Display integer of an array using for loop

class Disp_Array_ele{
public static void main (String args[]){
int arr[]=new int[]{2,4,6,8,10,12}; 
//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
for(int i=0; i<arr.length; i++){
System.out.print(arr[i]+" ");

}
}
}


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

Display elements of given array
2,4,6,8,10,12

 

Program to Display array in Java using while loop

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

Program 2

//Display integer of an array using while loop

class Disp_Array_ele1{
public static void main (String args[]){
int arr[]=new int[]{21,24,64,68,10,12}; 
//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.print(arr[i]+" ");
 i++;
}
}
}

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

Display elements of given array
21,24,64,68,10,12

 

Program to Display array in Java using do-while loop

In this program, we are briefing how to  print integer array elements using do-while loop in Java language

Program 3

//Display integer of an array using do-while loop

class Disp_Array_ele2{
public static void main (String args[]){
int arr[]=new int[]{210,241,264,268,210,212}; 
//diclaration and initialization of an array

System.out.println("Display elements of given array");

//do-while loop use to displays the elements by incrementing value of i
int i=0; 
do{
System.out.print(arr[i]+" ");
 i++;
}while(i<arr.length);
}
}

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

Display elements of given array
210,241,264,268,210,212

 

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

 

 

 

 

Read and print Characters of one dim array in Java language
Program to Display String 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

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

2 days ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

5 days ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 week ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 weeks ago

C# inverted full pyramid star pattern

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

1 month ago

C# Full Pyramid star pattern program

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

2 months ago