Introduction To Arrays

Introduction To Arrays

  • We will learn how to use arrays in Java in this article. Examples will be used to demonstrate how to declare, initialise, and access array elements.

  • An Array is a Data Structure which stores a collection of homogenous items (similar kinds of items).

  • It follows 0-Based indexing and there is a contiguous allocation of memory.

  • Why do we use arrays? We can create an array of the string type that can hold 100 names if we need to record the names of 100 different persons rather than declaring 100 variables.

How to Declare Arrays in Java

  • Here is how to declare an array in Java.

    dataType[] arrayName = new dataType[Size of Array];
    
  • dataType - it can be a primitive data type like int, char, String, etc. or Java Objects

  • arrayName - It is the name that we assign to our array

  • Example

    // It can hold 10 entries
    int[] data = new int[10]
    

How to Initialize Arrays in Java?

Arrays can be initialized in two ways

  1. Directly assign values using curly braces:

    int[] numsArray = {1,2,3,4,5,6,7,8,9}
    
  2. We can also initialize arrays in Java, using the index number:

      int array[] = new int[5];
            array[0]= 5;
            array[1]= 10;
            array[2]= 15;
            array[3]= 20;
            array[4]= 25;
    

How to Access Elements of an Array in Java?

  • An array's element can be accessed using the index number. The syntax for accessing an array's elements is shown below:

            /* Defining an array */
            int[] ages = new int[3];
    
            // Assigning Values to Array indexes
            ages[0] = 5;
            ages[1] = 10;
            ages[2] = 15;
    
            // Printing the array
            System.out.println("Age at index 0: "+ages[0]);
            System.out.println("Age at index 1: "+ages[1]);
            System.out.println("Age at index 2: "+ages[2]);
    
  • Utilizing for - loop will make accessing elements of an array faster. For Example:

      int array[] = new int[5];
            array[0]= 5;
            array[1]= 10;
            array[2]= 15;
            array[3]= 20;
            array[4]= 25;
    
            for (int i=0; i< array.length; i++){
                System.out.println("Element at index "+ i + " is "+array[i]);
            }
    

Accessing elements using for-each loop:

 // create an array
   int[] age = {10, 12, 23, 34, 55};

   // loop through the array using for-each loop
   System.out.println("Using for-each Loop:");
   for(int a : age) {
     System.out.println(a);
   }

Sample Programs

  • Program 1: Program to find the sum of all the elements in an array.
private static void Question1(){

        int example1[] = {15, 33, 55, 70, 96};
        int sum =0;

        for (int i=0; i< example1.length; i++){
            sum = sum + example1[i];
        }

        System.out.println("Sum of all elements in array is "+sum);
    }
  • Program 2: Program to find the maximum element in an array.

    ```java private static void Question2(){

        int example2[] = {15, 33, 55, 100, 96};
        int max = -1;
    
        for (int i=0; i< example2.length; i++){
            if (example2[i]>max){
                max = example2[i];
            }
        }
    
        System.out.println("Maximum element is "+max);
    }
    
```
  • Program 3: Program to find a given element in an array. Element is present only once in the array

     private static void Question3(){
            int example3[] = {15, 33, 55, 100, 96};
            int element = 35;
            int ans = -1;
    
            for (int i=0; i< example3.length; i++){
                if (example3[i]== element){
                    ans = i;
                    break;
                }
            }
            if (ans ==-1){
                System.out.println("Element not found ");
            }
            else {
                System.out.println("element found at index "+ans);
            }
        }