Single Dimensional Array

Types of Array in java:

  • Single Dimensional Array.
  • Multidimensional Array.

Single Dimensional Array in Java:

A single-dimensional array in Java is a collection of elements of the same data type arranged in a linear order. Each element in the array is identified by an index, starting from 0 for the first element and going up to (array length – 1) for the last element. The syntax for declaring and initializing a single-dimensional array in Java is as follows:

Syntax for declaring a single-dimensional array:

dataType[] arrayName;

Syntax for creating and initializing a single-dimensional array:

dataType[] arrayName = new dataType[arrayLength];

Here, dataType represents the data type of the elements the array will hold. arrayName is the name of the array variable, and arrayLength is an integer value representing the number of elements the array can store.

Let’s create a simple example of declaring and initializing an array of integers:

public class SingleDimensionalArrayExample {
    public static void main(String[] args) {
        // Declare an array of integers
        int[] numbers;

        // Create and initialize the array with 5 elements
        numbers = new int[5];

        // Assign values to individual elements
        numbers[0] = 10;
        numbers[1] = 20;
        numbers[2] = 30;
        numbers[3] = 40;
        numbers[4] = 50;

        // Access and print elements
        System.out.println("Element at index 0: " + numbers[0]); // 10
        System.out.println("Element at index 2: " + numbers[2]); // 30

        // Modify an element
        numbers[1] = 200;

        // Print the updated element
        System.out.println("Updated element at index 1: " + numbers[1]); // 200
    }
}

Explanation:

  1. public class SingleDimensionalArrayExample : This line declares a public class named SingleDimensionalArrayExample. In Java, every program must have a class with a main method to serve as the entry point of the program.
  2. public static void main(String[] args) : This line defines the main method, which is the starting point of the program’s execution. It takes an array of strings (String[] args) as its parameter, which allows you to pass command-line arguments to the program.
  3. int[] numbers; : This line declares a reference variable numbers of type int[] (integer array). At this point, no memory is allocated for the array, and numbers is null.
  4. numbers = new int[5]; : This line creates and initializes the integer array numbers with a size of 5. Now, numbers refers to an actual array object in memory with five slots to hold integers.
  5. numbers[0] = 10; : This line assigns the value 10 to the first element of the numbers array, which is at index 0.
  6. numbers[1] = 20; : This line assigns the value 20 to the second element of the numbers array, which is at index 1.
  7. numbers[2] = 30; : This line assigns the value 30 to the third element of the numbers array, which is at index 2.
  8. numbers[3] = 40; : This line assigns the value 40 to the fourth element of the numbers array, which is at index 3.
  9. numbers[4] = 50; : This line assigns the value 50 to the fifth element of the numbers array, which is at index 4.
  10. System.out.println(“Element at index 0: ” + numbers[0]); : This line prints the value of the first element of the numbers array, which is 10.
  11. System.out.println(“Element at index 2: ” + numbers[2]); : This line prints the value of the third element of the numbers array, which is 30.
  12. numbers[1] = 200; : This line modifies the value of the second element of the numbers array (at index 1) from 20 to 200.
  13. Sytem.out.println(“Updated element at index 1: ” + numbers[1]); : This line prints the updated value of the second element of the numbers array, which is now 200.

In summary, this Java program demonstrates the creation, initialization, and manipulation of a single-dimensional integer array numbers. It sets the values of the elements at specific indices, accesses and prints their values, and modifies one of the elements.

Output:

Element at index 0: 10

Element at index 2: 30

Updated element at index 1: 200

 

Let’s come up with a different array example:

public class SingleDimensionalArrayExample {

    public static void main(String[] args) {
                
        double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5}; 
        double result = 0;
        int i; 
        for(i=0; i<5; i++) { 
            result = result + nums[i]; 
            }
        System.out.println("Average is " + result / 5);
    }
}

Explanation:

  1. public class SingleDimensionalArrayExample : This line declares a public class named SingleDimensionalArrayExample. In Java, every program must have a class with a main method to serve as the entry point of the program.
  2. public static void main(String[] args) : This line defines the main method, which is the starting point of the program’s execution. It takes an array of strings (String[] args) as its parameter, which allows you to pass command-line arguments to the program.
  3. double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5}; : This line declares and initializes a double array named nums with the values {10.1, 11.2, 12.3, 13.4, 14.5}. The size of the array is automatically determined based on the number of elements provided.
  4. double result = 0; : This is line declares a scalar variable named result of type double and initializes it with an initial value of 0. This variable will be used to store the sum of the elements in the nums array.
  5. int i; : This line declares an integer variable named i, which will be used as a loop counter in the following for loop.
  6. for (i = 0; i < 5; i++) { : This line starts a for loop that initializes i to 0, executes the loop body as long as i is less than 5, and increments i by 1 after each iteration. The loop will iterate five times because i starts at 0, and it stops when i becomes 4 (less than 5).
  7. result = result + nums[i]; : This line adds the value of the element at index i of the nums array to the result variable on each iteration of the loop. In each iteration, it accumulates the sum of all elements in the nums array.
  8. System.out.println(“Average is ” + result / 5); : This line calculates the average of the elements in the nums array by dividing the sum (result) by the number of elements (5). It then prints the result to the console.

In summary, this Java program calculates the average of the elements in the nums array using a for loop and prints the result to the console.

Output:

Average is 12.299999999999999

 

Visited 1 times, 1 visit(s) today

Comments are closed.

Close