Table of Contents
Print one dimensional integer array in Java
In this article, we will discuss the concept of Print one dimensional integer array in Java language
In this post, we are going to learn how to write a program to display integer elements in single dimensional array in Java language using for, while and do-while loop
Code to display integer array
Code to display integers in an array using for loop – #1
In this code, we are going to learn how to print integer elements in the single dimensional array using for loop in Java language
Program 1
//Display integer of an array using for loop class Disp_Array_element_int1{ public static void main (String args[]){ int arr[]=new int[6]; //declaration and creation of an array arr[0]=120;//initialization of int array arr[1]=130; arr[2]=140; arr[3]=150; arr[4]=160; arr[5]=170; //display array elemnts of int 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 the given array 120 130 140 150 160 170
Code to display integers in an array using for loop – #2
In this code, we are going to learn how to print integer elements in single dimensional array using for loop in Java language
Program 2
import java.util.Scanner; public class AcceptArrayInputforA{ public static void main(String args[]){ //scanner class to read input from the user Scanner sc=new Scanner(System.in); System.out.print("Enter array length"); int len=sc.nextInt(); //Declaring and creating array int[] arr=new int[len]; //initializing value to the array System.out.println("******Initializing elements of the array******"); System.out.println("Enter "+arr.length+" integer values"); for(int i=0; i<arr.length; i++){ System.out.print("arr["+i+"]:"); arr[i]=sc.nextInt(); }//using the for loop to initializing array //displaying the array System.out.println("\n******displaying given array elements******"); System.out.println("Entered array elements are"); for(int i=0; i<arr.length; i++){ System.out.print("arr["+i+"]:"); System.out.println(arr[i]+"\t"); } } }
When the above code is executed, it produces the following result
Enter array length: 4 ******Initializing 6 elements of the array******" Enter 4 integer value arr[0]: 125 arr[1]: 525 arr[2]: 650 arr[3]: 725 ******displaying given 6 array elements****** Entered array elements are: arr[0]: 125 arr[1]: 525 arr[2]: 650 arr[3]: 725
Code to display integers in an array using while loop – #1
In this code, we are going to learn how to print integer elements in single dimensional array using while loop in Java language
Program 1
//Display integer of an array using for loop class Disp_Array_element_intwhile{ public static void main (String args[]){ int arr[]=new int[6]; //declaration and creation of an array arr[0]=12;//initialization of int array arr[1]=113; arr[2]=214; arr[3]=315; arr[4]=416; arr[5]=517; //display array elements of int 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 113 214 315 416 517
Code to display integers in an array using while loop – #2
In this code, we are going to learn how to print integer elements in single dimensional array using while loop in Java language
Program 2
import java.util.Scanner; public class DisplayArrayInputwhileA{ public static void main(String args[]){ //scanner class to read input from the user Scanner sc=new Scanner(System.in); System.out.print("Enter array length"); int len=sc.nextInt(); //Declaring and creating array int[] arr=new int[len]; //initializing value to the array System.out.println("******Initializing elements of the array******"); System.out.println("Enter "+arr.length+" integer values"); int i=0; while(i<arr.length){ System.out.print("arr["+i+"]:"); arr[i]=sc.nextInt(); i++; }//using the while loop to initializing array //displaying the array System.out.println("\n******displaying given array elements******"); System.out.println("Entered array elements are"); i=0; while( i<arr.length){ System.out.print("arr["+i+"]:"); System.out.println(arr[i]+"\t"); i++; } } }
When the above code is executed, it produces the following result
Enter array length: 5 ******Initializing 6 elements of the array******" Enter 5 integer value arr[0]: 150 arr[1]: 250 arr[2]: 350 arr[3]: 450 arr[4]: 550 ******displaying given 6 array elements****** Entered array elements are: arr[0]: 150 arr[1]: 250 arr[2]: 350 arr[3]: 450 arr[4]: 550
Code to display integers in an array using do-while loop – #1
In this code, we are going to learn how to print integer elements in single dimensional array using do-while loop in Java language
Program 1
//Display integer of an array using for loop class Disp_Array_element_intdowhile{ public static void main (String args[]){ int arr[]=new int[6]; //declaration and creation of an one dim array arr[0]=125;//initialization of int array arr[1]=135; arr[2]=145; arr[3]=155; arr[4]=165; arr[5]=175; //display array elements of int 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 125 135 145 155 165 175
Code to display integers in an array using do-while loop – #2
In this code, we are going to learn how to print integer elements in single dimensional array using do-while loop in Java language
Program 2
import java.util.Scanner; public class DisplayArrayInputdowhileA{ public static void main(String args[]){ //scanner class to read input from the user Scanner sc=new Scanner(System.in); System.out.print("Enter array length"); int len=sc.nextInt(); //Declaring and creating array int[] arr=new int[len]; //initializing value to the array System.out.println("******Initializing elements of the array******"); System.out.println("Enter "+arr.length+" integer values"); int i=0; do{ System.out.print("arr["+i+"]:"); arr[i]=sc.nextInt(); i++; }while(i<arr.length);//using the do-while loop to initializing array //displaying the array System.out.println("\n******displaying given array elements******"); System.out.println("Entered array elements are"); i=0; do{ System.out.print("arr["+i+"]:"); System.out.println(arr[i]+"\t"); i++; }while( i<arr.length); } }
When the above code is executed, it produces the following result
Enter array length: 3 ******Initializing 3 elements of the array******" Enter 3 integer value arr[0]: 1503 arr[1]: 2504 arr[2]: 3505 ******displaying given 3 array elements****** Entered array elements are: arr[0]: 1503 arr[1]: 2504 arr[2]: 3505
Suggested for you
Variable in C language
Data type in C language
One dimensional array in C language
Variable in C++ language
Data type in C++ language
One dimensional array in C++ language
Do-while loop in Java language
Similar post
Java program to read and display array elements using for loop
Java program to read and display array elements using while loop
Java program to read and display 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