Java program to read and print elements of one dim array
Java program to read and print elements of one dim array
In this tutorial, we will discuss the concept of Java program to read and print elements of one dimensional array
In this topic, we are going to learn how to read and print elements of a one dimensional array 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
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)
Here,
- e1,e2,e3,e4 and e5 represent the elements of the array
- 0,1,2,3,4 represent the index of the array which is used to access the array elements
- 1,2,3,4,5 represent the length of the array
Code to read and print elements of one dim array
Read and print elements Using for loop
In this program, we are briefing input elements of an array and then print them using for loop in Java language
Program 1
//Java program to read and print elements a one dimensional array import java.util.Scanner; class Array_Single_Dim{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the Array length: "); int len=scan.nextInt(); int arr[]=new int[len]; //declaration and creation i dim array System.out.println("Enter the array elements"); for(int i=0; i<len; i++){ arr[i]=scan.nextInt(); } System.out.println("\narray elements are:"); for(int i=0; i<len; i++){ System.out.println(arr[i]); }//display every array element } }
When, the above code is executed, it produces the following result
Program 2
In this program, we are briefing input elements of an array and then print them using for loop in Java language
//Java program to read and print elements a one dimensional array import java.util.Scanner; class Array_Single_Dim1{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the Array length: "); int len=scan.nextInt(); int arr[]=new int[len]; //declaration and creation 1 dim array System.out.println("Enter the array elements"); for(int i=0; i<len; i++){ System.out.print("[arr["+i+"]:"); arr[i]=scan.nextInt(); } System.out.println("\narray elements are:"); for(int i=0; i<len; i++){ System.out.println("[arr["+i+"]:"+arr[i]); }//display every array element } }
When, the above code is executed, it produces the following result
Read and print elements Using while loop
In this program, we are briefing input elements of an array and then print them using while loop in Java language
Program 1
//Java program to read and print elements a one dimensional array import java.util.Scanner; class Array_Single_Dimwhile{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the Array length: "); int len=scan.nextInt(); int arr[]=new int[len]; //declaration and creation i dim array System.out.println("Enter the array elements"); int i=0; while(i<len){ arr[i]=scan.nextInt(); i++; } System.out.println("\narray elements are:"); i=0; while(i<len){ System.out.println(arr[i]); i++; }//display every array element } }
When, the above code is executed, it produces the following result
Program 2
In this program, we are briefing input elements of an array and then print them using while loop in Java language
//Java program to read and print elements a one dimensional array import java.util.Scanner; class Array_Single_Dimwhile1{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the Array length: "); int len=scan.nextInt(); int arr[]=new int[len]; //declaration and creation 1 dim array System.out.println("Enter the array elements"); int i=0; while(i<len){ System.out.print("[arr["+i+"]:"); arr[i]=scan.nextInt(); i++; } System.out.println("\narray elements are:"); i=0; while(i<len){ System.out.println("[arr["+i+"]:"+arr[i]); i++; }//display every array element } }
When, the above code is executed, it produces the following result
Read and print elements Using do-while loop
In this program, we are briefing input elements of an array and then print them using do-while loop in Java language
Program 1
//Java program to read and print elements a one dimensional array import java.util.Scanner; class Array_Single_DimDowhile{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the Array length: "); int len=scan.nextInt(); int arr[]=new int[len]; //declaration and creation 1 dim array System.out.println("Enter the array elements"); int i=0; do{ arr[i]=scan.nextInt(); i++; }while(i<len); //reading every array element System.out.println("\narray elements are:"); i=0; do{ System.out.println(arr[i]); i++; }while(i<len); //display every array element } }
When, the above code is executed, it produces the following result
Program 2
In this program, we are briefing input elements of an array and then print them using do-while loop in Java language
//Java program to read and print elements a one dimensional array import java.util.Scanner; class Array_Single_DimDowhile1{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); //create a scanner instance for receives input // from the user - input from keyboard System.out.print("Enter the Array length: "); int len=scan.nextInt(); int arr[]=new int[len]; //declaration and creation 1 dim array System.out.println("Enter the array elements"); int i=0; do{ System.out.print("[arr["+i+"]:"); arr[i]=scan.nextInt(); i++; }while(i<len); System.out.println("\narray elements are:"); i=0; do{ System.out.println("[arr["+i+"]:"+arr[i]); i++; }while(i<len); //display every array element } }
When, the above code is executed, it produces the following result
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
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
Operator in Java language
Data type and variable in Java language