All the Notes in one Place. Download our Official App from Google Play.

Java Arrays

                                       Java Arrays

An array is a group of like-typed variables that are referred to by a common name. Arrays of any type can be created and may have one or more dimensions. A specific element in an array is accessed by its index. Arrays offer a convenient means of grouping related information.

One-Dimensional Arrays:

A one-dimensional array is, essentially, a list of like-typed variables. To create an array, you first must create an array variable of the desired type. The general form of a one-dimensional array declaration is

type var-name[];

Type declares the base type of the array. The base type determines the data type of each element that comprises the array. For example, following array named month_days with data type of int:

int month_days[];

This declaration establishes the fact that month_days is an array variable, no array actually exists. In fact, the value of month_days is set to null, which represents an array with no value. To link month_days with an actual, physical array of integers, you must allocate one using new and assign it to month_days. new is a special operator that allocates memory. The general format is:

array-var = new type[size];

Here, type specifies type of data, size specifies the number of elements in the array, and array-var is the array variable that is linked to the array.

month_days = new int[12];

Here an exp that calculates average using one-dimensional array.
Copy After this Line:

import java.util.*;
public class Average{
    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);


        }


}
Output of this Program:

Java Program Calculating Average using Array by Asad Hussain

Multidimensional Arrays:

In Java, multidimensional arrays are actually arrays of arrays. These, as you might expect, look and  act like regular multidimensional arrays. However, as you will see, there are a couple of subtle differences. To declare a multidimensional array variable, specify each additional index using another set of square brackets, For example, the following declares a two-dimensional array variable called multiD.

int multiD [][] = new int [4][5];

This allocates a 4 by 5 array and assigns it to multiD. Internally this matrix is implemented as an array of arrays of int.

Here an exp that uses multidimensional array.
Copy After this Line:
/*
         This is a Simple Program Showing Use Multi Dim Array.
          Program developed by #Asad Hussain
         This file name is "MultiD.java".
*/
import java.util.*;
public class MultiD{
    public static void main(String args[]){
    int multiD[][] = new int[4][5];
    int i, j, k = 0;
  
    for(i = 0; i < 4; i++)
        for(j = 0; j < 5; j++)    {
            multiD[i][j] = k;
        k++;

    }

    for(i = 0; i < 4; i++) {
        for(j = 0; j < 5; j++)  
            System.out.println(multiD[i][j] +" ");
        System.out.println();
      
    }
        }


}
 
Output of this Program:
Java Program showing use of Multidimensional array by Asad Hussain

Written By: Asad Hussain

Post a Comment

Previous Post Next Post