One Dimensional Array in Java with Examples

In this blog post I will be sharing One Dimensional Array Program in Java with examples. One dimensional array is also known as single dimensional array. But before moving ahead we should know what is an Array.

Read Also: Top 50 Array Interview Questions and Answers in Java

1. What is an Array


An Array is a collection of homogeneous elements stored in a contiguous memory.

2. What is One Dimensional Array in Java


Array can be one dimensional or multi-dimensional. Anything that is one dimensional has to deal with single parameter only. One dimensional array has to deal with only one value per index or location.

3. Examples of One Dimensional Array in Java


Three examples of One Dimensional Array in Java are given below:

3.1 One Dimensional Array Example in Java Using Standard Method



public class OneDimensionalArray {
    public static void main(String args[]) {
        
      // Declares an One Dimensional Array of integers
      int [] anArray;
      
      // Allocates memory for 3 integers
      anArray = new int[3];
      
      // initializes first element 
      anArray[0] = 8;
      // initializes second element 
      anArray[1] = 4;
      // initializes third element 
      anArray[2] = 89;

      // Printing the One Dimensional Array
      System.out.println("One dimensional array elements are :");
      System.out.println("Element at index 0: "+ anArray[0]);
      System.out.println("Element at index 1: "+ anArray[1]);
      System.out.println("Element at index 2: "+ anArray[2]);
    }
}


Output:
One dimensional array elements are :
Element at index 0: 8
Element at index 1: 4
Element at index 2: 89

3.2 One Dimensional Array Example in Java Using Scanner



import java.util.Scanner;
public class OneDimensionalArrayScanner {
    public static void main(String args[]) 
    {
      // Creating Scanner Object
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter length of Array: ");
      int arrLength = scan.nextInt();
      int [] anArray= new int[arrLength]; 
      System.out.println("Enter the elements of the Array");
      for(int i = 0; i < arrLength; i++)
      {
          anArray[i] = scan.nextInt();
      }
      // Printing the One Dimensional Array
      System.out.println("Displaying One dimensional array elements:");
      for( int i=0; i < arrLength ; i++)
      {
          System.out.print(anArray[i] + " ");
      }      
    }
}

Output:
Enter length of Array: 4
Enter the elements of the Array
8
4
89
2
Displaying One dimensional array elements:
8 4 89 2


3.3 One Dimensional Array Example in Java Using String



public class OneDimensionalArrayString {
    public static void main(String args[]) 
    {
      // Declaring and Initializing the String Array
      String[] strArray = {"Alive is Awesome", "Be in Present","Be Yourself"};
      System.out.println("The length of String Array is: "+strArray.length);
      // Printing the One Dimensional String Array
      System.out.println("Displaying One dimensional String array elements:");
      for( int i=0; i < strArray.length ; i++)
      {
          System.out.println(strArray[i] + " ");
      }      
    }
}


Output:
The length of String Array is: 3
Displaying One dimensional String array elements:
Alive is Awesome
Be in Present
Be Yourself

That's all for today, please mention in comments in case you have any other questions related to One Dimensional Array in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry