Java Program to Print the Elements of an Array
An array is to be created in java and elements will be stored in it. After successful insertion, all the elements of the array are printed present in the array.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. Users can access the elements simply by referring to the index number of the first element inserted. It is because of the traits of this data structure consisting of a group of like-typed variables in sequential order in the memory.that are referred to by a common name element by referring to the index number. In java, arrays do work differently as they do work in C/C++.
- In Java, all arrays are dynamically allocated.
- Since arrays are objects in Java, user can find their length using the object property length. This is different from C/C++ where length is calculated using function sizeof()
- A Java array variable can also be declared like other variables with [] after the data type.
- The variables in the array are ordered and each has an index beginning from 0.
- Java array can be also be used as a static field, a local variable, or a method parameter.
- The size of an array must be specified by an int value and not long or short.
- The direct superclass of an array type is Object.
- Every array type implements the interfaces Cloneable and java.io.Serializable.
Examples:
array_mame = {2 , 7 , 4 , 1 , 4} Output: 2 7 4 1 4 array_name = {2 , 7, -1 , 6 , -3} Output: 2 7 -1 6 -3
Approaches:
- Using loops
- Using standard library arrays
Approach 1: Printing elements of an array using loops
Algorithm:
- Declare and initialize an array
- Loop through the array by incrementing the value of the iterative variable/s
- Print out each element of the array
Implementation:
Below is the java example illustrating printing elements of an array
Java
// Java Program to Print the Elements of an Array // Using loops (considering for loop here) public class GFG { // Main driver method public static void main(String[] args) { // Initialize array of random numbers and size // Suppose array named 'arr' contains 9 elements int [] arr = { - 7 , - 5 , 5 , 10 , 0 , 3 , 20 , 25 , 12 }; System.out.print( "Elements of given array are: " ); // Looping through array by incrementing value of i //'i' is an index of array 'arr' for ( int i = 0 ; i < arr.length; i++) { // Print array element present at index i System.out.print(arr[i] + " " ); } } } |
Output :
Elements of given array are: -7 -5 5 10 0 3 20 25 12
Time Complexity: O(n) Here other no major execution is taking place except just the cell memory taken by variables that even get destroyed as the scope is over. Whenever there is iteration just by using one loop time taken is of the order of n always. If nested then the order of number of loops that are nested
Space Complexity: O(n) As whatever loop is used considering the worst case where the complete array is filled up, so it takes up simply the space taken by the array in memory.
Approach 2: Printing elements of an array using standard library arrays
Algorithm:
- Declare and initialize an array
- Use Arrays.toString() function inside the print statement to print array
Implementation:
Java Program to print the elements of an array using standard library arrays:
Java
// Java Program to Print the Elements of an Array // Importing specific array class // so as to use inbuilt functions import java.util.Arrays; public class GFG { // Main driver method public static void main(String[] args) { // Initialize array // Array 'arr' contains 9 elements int [] arr = { - 7 , - 5 , 5 , 10 , 0 , 3 , 20 , 25 , 12 }; System.out.print( "Elements of given array are: " ); // Pass the array 'arr' in Arrays.toString() // function to print array System.out.println(Arrays.toString(arr)); } } |
Output :
Elements of given array are: [-7, -5, 5, 10, 0, 3, 20, 25, 12]
Time Complexity: O(n)
Space Complexity: O(n)
Please Login to comment...